如何在xslt中使用xsl:for-each获取所有子元素标记名称及其值

时间:2014-09-09 06:20:19

标签: xml xslt

我有像这样的xml,

<item-info>    
    <jid>CQG</jid>    
    <aid>498781</aid>    
    <ce:pii>10.10-88/0(26)49381-/0/0/000000</ce:pii>    
    <ce:doi>10.1088/02649381/0/0/000000</ce:doi>    
    <ce:copyright type="unknown" year="2014"></ce:copyright>    
</item-info>

我正在使用xslt代码。

<xsl:template match="item-info">
    <xsl:element name="item-info">
        <jid><xsl:value-of select="/article/item-info/jid"/></jid>
        <aid><xsl:value-of select="/article/item-info/aid"/></aid>
        <ce:pii><xsl:value-of select="/article/item-info/ce:pii"/></ce:pii>
        <ce:doi><xsl:value-of select="/article/item-info/ce:doi"/></ce:doi>
        <ce:copyright> 
            <xsl:attribute name="type">
                <xsl:value-of select="/article/item-info/ce:copyright/@type"/>
            </xsl:attribute>        
            <xsl:attribute name="year">
                <xsl:value-of select="/article/item-info/ce:copyright/@year"/>
            </xsl:attribute>
        </ce:copyright>
    </xsl:element>
</xsl:template>

我可以做些什么才能在单xsl:value-of选择中获取所有子名称及其值,而不是提及每一个?

有可能吗?

1 个答案:

答案 0 :(得分:0)

有很多方法可以做到。我经常使用所有子节点的copy和apply-templates:

<xsl:template match="item-info">
    <xsl:copy>
        <xsl:apply-templates select="*|@*|text()"></xsl:apply-templates>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="*|@*|text()"/>
    </xsl:copy>
</xsl:template>

您也可以只选择您想要的孩子:

<xsl:template match="item-info">
    <xsl:copy>
        <xsl:apply-templates select="jid|aid|ce:pii|cd:doi|ce:copyright"></xsl:apply-templates>
    </xsl:copy>
</xsl:template>