如何使用XSLT转换XML添加子节点?

时间:2016-02-16 10:21:47

标签: xml xslt

我有以下XML文档作为输入( InputXML )到Java程序,该程序应用XSL( TransformationXSL )转换为输出XML( OutputXML )。

我想添加一个额外的节点作为输入XML转换的一部分。有没有比( TranformationXSL )中显示的更好的方法,因为这只是试图匹配元素并复制所需的内容???任何其他有效的方式/建议都非常感谢。

InputXML

<?xml version="1.0" encoding="UTF-8"?>
<tuple>
    <old>
        <Customers>
            <OrderID>10248</OrderID>
            <CustomerID>VINET</CustomerID>
            <EmployeeID>8</EmployeeID>
            <OrderDate>1996-07-04T00:00:00.0</OrderDate>
            <CustomerID>VINET</CustomerID>
            <CompanyName>Vins et alcools Chevalier</CompanyName>
        </Customers>
    </old>
</tuple>

OutputXML

<?xml version="1.0" encoding="UTF-8"?>
<tuple>
    <old>
        <Customers>
            <Orders>
                <OrderID>10248</OrderID>
                <CustomerID>VINET</CustomerID>
                <EmployeeID>8</EmployeeID>
                <OrderDate>1996-07-04T00:00:00.0</OrderDate>
            </Orders>
            <CustomerID>VINET</CustomerID>
            <CompanyName>Vins et alcools Chevalier</CompanyName>                
        </Customers>
    </old>
</tuple>

这是我所说的 TransformationXSL 。可以修改它以有效地转换输入XML以提供所需的输出XML ???

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" omit-xml-declaration="no" indent="yes" />

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

    <xsl:template match="Customers">
        <Customers>
            <Orders>
                <OrderID>10248</OrderID>
                <CustomerID>VINET</CustomerID>
                <EmployeeID>8</EmployeeID>
                <OrderDate>1996-07-04T00:00:00.0</OrderDate>
            </Orders>
            <CustomerID>VINET</CustomerID>
            <CompanyName>Vins et alcools Chevalier</CompanyName>
        </Customers>
    </xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:2)

您显示的输出可以通过以下方式轻松获得:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Customers">
    <xsl:copy>
        <Orders>
            <xsl:copy-of select="OrderID | CustomerID[1] | EmployeeID | OrderDate"/>
        </Orders>
        <xsl:copy-of select="CustomerID[1] | CompanyName"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:1)

如果 InputXML 中的双标记CustomerID出错,请检查我的解决方案。

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

    <xsl:output method="xml"
                version="1.0"
                encoding="utf-8"
                indent="yes"
                standalone="yes"
                omit-xml-declaration="no"/>

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

    <xsl:template match="OrderID">
        <xsl:element name="Orders">
            <xsl:element name="OrderID">
                <xsl:value-of select="../OrderID/text()"/>
            </xsl:element>
            <xsl:element name="CustomerID">
                <xsl:value-of select="../CustomerID/text()"/>
            </xsl:element>
            <xsl:element name="EmployeeID">
                <xsl:value-of select="../EmployeeID/text()"/>
            </xsl:element>
            <xsl:element name="OrderDate">
                <xsl:value-of select="../OrderDate/text()"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <xsl:template match="EmployeeID"/>
    <xsl:template match="OrderDate"/>
</xsl:stylesheet>