新XSLT - 如何返回跨度类的文字XML?(New to XSLT - how to retur

2019-10-20 05:08发布

我有以下XML:

    <core:renderedItem>
    <span class="harvard_title">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</span>
    '
    <span>
      <em>Journal of Human Rights and the Environment</em>
    </span>
    .
  </div>

我如何获得跨类=“harvard_title”使用XSLT的文字? 理想情况下,我想起来只返回文本冒号“谁的生态系统是它无论如何”

Answer 1:

假设这个XML(您发布的一个不能很好地形成的):

<div xmlns:core="http://www.w3.org/core">
    <core:renderedItem>
        <span class="harvard_title">Whose Ecosystem is it Anyway: Private and Public Rights under New Approaches to Biodiversity Conservation</span>
        '
        <span>
            <em>Journal of Human Rights and the Environment</em>
        </span>
        .
    </core:renderedItem>
</div>

你可以得到你想要的这个XSL代码文本:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:core="http://www.w3.org/core"
  exclude-result-prefixes="xsl">
  <xsl:output omit-xml-declaration="yes" method="xml" indent="yes" />

  <xsl:template match="/">
    <xsl:value-of select="substring-before(div/core:renderedItem/span[@class = 'harvard_title'], ':')" />
  </xsl:template>

</xsl:stylesheet>

蒙山这样的: span[@class = 'harvard_title']你得到的跨度内的文本,并与substring-before的功能,你得到的只是去冒号前的文本。

希望能帮助到你。

编辑:您可以使用if考虑到那里是不是在文本中的冒号的话,但你可以使用模板做到这一点:

<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:core="http://www.w3.org/core"
  exclude-result-prefixes="xsl">
  <xsl:output omit-xml-declaration="yes" method="xml" indent="yes" />

  <xsl:template match="/">
    <xsl:apply-templates select="div/core:renderedItem/span[@class = 'harvard_title']" />
  </xsl:template>

  <!-- generic template, for span elements with class = 'harvard_title' -->
  <xsl:template match="span[@class = 'harvard_title']">
    <xsl:value-of select="." />
  </xsl:template>
  <!-- more specific template, for those ones containing a colon -->
  <xsl:template match="span[@class = 'harvard_title'][contains(.,':')]">
    <xsl:value-of select="substring-before(., ':')" />
  </xsl:template>

</xsl:stylesheet>


文章来源: New to XSLT - how to return text of span class in xml?
标签: xml xslt html