XSLT获取空TAG名称

时间:2019-03-16 08:32:17

标签: xslt xpath xslt-1.0 xslt-2.0

我需要找出这两个人的专业,可以选择出现不同的行业。所以我想找出所有像这样的空标签的名称:

<artists>
    <artist name="Ghiberti">
      <name>Lorenzo Ghiberti </name>
      <birth>1378</birth>
      <death>1455</death>
      <goldsmith /> <!-- This -->
      <sculptor /> <!-- This -->
    </artist>
    <artist name="Donatello">
      <name>Donato di Niccolò di Betto Bardi</name>
      <birth>1386</birth>
      <death>1466</death>
      <sculptor /> <!-- This -->
    </artist>
 </artists>

我已经完成了结构,并且得到了该人的所有数据,但是没有得到该标签的名称。我想得到这个:

Miguel Ángel Buonarroti ( goldsmith, sculptor ) birth in 1475 and death in 1564
Giorgio Vasari (sculptor ) birth in 1511 and death in 1574

这是我的实际代码:

<xsl:template match="artists">
    <h2> Artist florecian </h2>
    <xsl:for-each select="artist">
      <xsl:value-of select="name"/> (
    birth in <xsl:value-of select="birth"/> and death in <xsl:value-of select="death"/> <br/>
    </xsl:for-each>    
      </xsl:template>

2 个答案:

答案 0 :(得分:2)

您的XML结构错误。再试一次:

<artists>
    <artist name="Ghiberti">
      <name>Lorenzo Ghiberti </name>
      <birth>1378</birth>
      <death>1455</death>
      <profession>goldsmith</profession>
      <profession>sculptor</profession>
    </artist>
    <artist name="Donatello">
      <name>Donato di Niccolò di Betto Bardi</name>
      <birth>1386</birth>
      <death>1466</death>
      <profession>sculptor</profession>
    </artist>
 </artists>

请考虑为什么选择<name>...</name>而不选择<profession>...</profession>。通过此更改,您的XSLT将变得非常简单,我确定您不需要此方面的帮助。

当然可以使用name() XPath function获取节点的名称,但是在这种情况下,这不是正确的解决方案。

答案 1 :(得分:1)

尝试:

XSLT 2.0

<xsl:template match="artists">
    <h2> Artist florecian </h2>
        <xsl:for-each select="artist">
            <xsl:value-of select="name"/>
            <xsl:text> (</xsl:text>
            <xsl:value-of select="*[not(text())]/name()" separator=", "/>
            <xsl:text>) birth in </xsl:text>
            <xsl:value-of select="birth"/>
            <xsl:text> and death in </xsl:text>
            <xsl:value-of select="death"/> 
            <br/>
    </xsl:for-each>    
</xsl:template>

演示:https://xsltfiddle.liberty-development.net/bFN1y98