查找父属性并附加到子级

时间:2015-04-18 10:15:11

标签: xml xslt

我需要转换一些xml文件,其中一个任务是在指定节点中查找属性并为其附加属性值 子节点。 例如,这里是原始的xml:

<ParentNode attribute="someValue">
    <ChildNode other_att="foo">
       <OtherNode />
    </ChildNode>   
</ParentNode>

这就是所需的输出:

<ParentNode attribute="someValue">
    <ChildNode other_att="foo" appended_attribute="someValue">
       <OtherNode />
    </ChildNode>   
</ParentNode>

我只是熟悉xslt。这是我试图做的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:template>

        <xsl:template match="ChildNode">
        <xsl:copy>
                <xsl:attribute name="appended_attribute">
                <xsl:value-of select="'someValue'"/>
                </xsl:attribute>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates select="node()"/>
            </xsl:copy>
        </xsl:template>
</xsl:stylesheet>

但它只选择了childnode并使用hardwired值附加属性。

任何有关建议的建议。 谢谢!

1 个答案:

答案 0 :(得分:0)

更改

    <xsl:template match="ChildNode">
    <xsl:copy>
            <xsl:attribute name="appended_attribute">
            <xsl:value-of select="'someValue'"/>
            </xsl:attribute>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ChildNode">
    <xsl:copy>
            <xsl:attribute name="appended_attribute">
            <xsl:value-of select="../@attribute"/>
            </xsl:attribute>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
相关问题