【问题标题】:Copy attribute from parent using xslt使用 xslt 从父级复制属性
【发布时间】:2012-05-31 01:51:30
【问题描述】:

我需要使用 XSLT 1.0 修改 XML。基本上,我需要将属性(名称和值)复制到一个孩子。

这是xml:

<parent id="3450"> <son1> <name>Malcom</name> <age>15</age> <description>This is the middle son</description> </son1> <son2> <name>Francis</name> <age>19</age> <description>This is the oldest son</description> </son2> <son3> <name>Dewey</name> <age>9</age> <description>This is the youngest son</description> </son3> </parent>

这应该是结果:

<parent id="3450"> <son1 id="3450"> <name>Malcom</name> <age>15</age> <description>This is the middle son</description> </son1> </parent>

这是我正在使用的 XSLT:

<xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="parent/son1"> <xsl:copy> <xsl:attribute name="id"><xsl:value-of select="../@id"/></xsl:attribute> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="parent/son2" /> <xsl:template match="parent/son3" />

XSLT 似乎在工作,但我的问题是:这是正确的做法吗?

谢谢。

【问题讨论】:

    标签: xslt


    【解决方案1】:

    我会改变

    <xsl:template match="parent/son1">    
        <xsl:copy>
            <xsl:attribute name="id"><xsl:value-of select="../@id"/></xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    

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

    【讨论】:

    • 太棒了,我不知道可以做这样的事情。谢谢!
    猜你喜欢
    • 2020-03-19
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多