XSLT-动态节点名称

时间:2019-02-11 04:45:08

标签: xslt-3.0

我想将所有子节点作为属性名称转换为Nodes。属性值应为节点值。

如何命名动态节点名称?

<xsl:attribute name="shouldBeNodeName">

源XML

<transaction>
    <transactionId>T001</transactionId>
    <clientId>C001</clientId>
    <contractId>C001</contractId>
    <scriptId>REL</scriptId>
    <price>500.5000</price>
    <tradeDate>2019-02-09 16:00:00</tradeDate>
    <valueDate>2019-02-09 16:00:00</valueDate>
    <quantity>100000</quantity>
</transaction>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <ROOT>
            <TRANS>
                <xsl:for-each select="node()/*">
                        <xsl:attribute name="shouldBeNodeName">
                            <!-- <xsl:value-of select="name()" /> -->   
                            <xsl:value-of select="." />
                        </xsl:attribute>
                </xsl:for-each>
            </TRANS>
        </ROOT>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <ROOT>
        <TRANS>
            <xsl:for-each select="node()/*">
                <xsl:attribute name="{name()}">
                        <xsl:value-of select="." />
                    </xsl:attribute>
            </xsl:for-each>
        </TRANS>
    </ROOT>
</xsl:template>