XML 转换:使用 xslt 从输入 xml 的节点属性在输出 xml 中创建节点

XML transformation: Creating node in output xml from node attribute of input xml using xslt

我搜索了很多网站都没有成功完成以下要求。希望在这里得到一些提示。

我想将输入 XML 以如下所示的特定格式转换为输出格式,输入 XML 中节点的属性用作输出中的节点XML

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<viewentries timestamp="20150429T170520,91Z" toplevelentries="19">
<viewentry position="18" unid="5952E3CE7079811485257DF8007535FF"
    noteid="10A36" siblings="19">
    <entrydata columnnumber="0" name="AssignedTo">
        <text>Nilesh</text>
    </entrydata>
    <entrydata columnnumber="1" name="ProductType">
        <text>LOC</text>
    </entrydata>
    <entrydata columnnumber="2" name="BranchNum">
        <text>1020</text>
    </entrydata>
    <entrydata columnnumber="3" name="CustomeName">
        <text>OldDate ¥§ª¬®°, DiptiÃÝ D'Ál®°</text>
    </entrydata>
    <entrydata columnnumber="4" name="ProductNum">
        <text>1234567</text>
    </entrydata>
    <entrydata columnnumber="5" name="Coverage">
        <text>LIFE</text>
    </entrydata>
    <entrydata columnnumber="6" name="DownloadDate">
        <text>02/26/2015 16:20:15</text>
    </entrydata>
    <entrydata columnnumber="7" name="UWAssignDate">
        <text>04/15/2015</text>
    </entrydata>

</viewentry>
<viewentry position="19" unid="65280F1D19102E1285257E29007436E5"
    noteid="10E02" siblings="19">
    <entrydata columnnumber="0" name="AssignedTo">
        <text>Peter Coy</text>
    </entrydata>
    <entrydata columnnumber="1" name="ProductType">
        <text>MTG</text>
    </entrydata>
    <entrydata columnnumber="2" name="BranchNum">
        <text>1020</text>
    </entrydata>
    <entrydata columnnumber="3" name="CustomeName">
        <text>DUPMERGENBMTG1, FLORANB1</text>
    </entrydata>
    <entrydata columnnumber="4" name="ProductNum">
        <text>8880811</text>
    </entrydata>
    <entrydata columnnumber="5" name="Coverage">
        <text>LIFE</text>
    </entrydata>
    <entrydata columnnumber="6" name="DownloadDate">
        <text>04/16/2015 17:09:22</text>
    </entrydata>
    <entrydata columnnumber="7" name="UWAssignDate">
        <text>04/16/2015</text>
    </entrydata>

</viewentry>

输出:

<?xml version="1.0"?>
<ROOT>

<ROW>

    <ROWNUM>1</ROWNUM>

    <Assignedto>Nilesh</Assignedto>

    <PRODUCTTYPE>LOC</PRODUCTTYPE>

    <BRANCH>1020</BRANCH>


</ROW>

<ROW>

    <ROWNUM>2</ROWNUM>

    <Assignedto>Peter Coy</Assignedto>      

    <PRODUCTTYPE>MTG</PRODUCTTYPE>

    <BRANCH>1020</BRANCH>


</ROW>
<ROOT>

使用下面的xslt可以实现上面的需求

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/viewentries">
    <xsl:for-each select="viewentry">
            <xsl:element name="ROW">
                <xsl:for-each select="entrydata">
                        <!-- &lt;<xsl:value-of select="@name" />&gt; -->



                        <xsl:variable name="assignedTo" select="'AssignedTo'"/>
                         <xsl:variable name="theSet" select="boolean(@name = $assignedTo)"/>
                        <xsl:if test="$theSet">
                        <AssignedTo><xsl:value-of select="text" /></AssignedTo>
                        </xsl:if>  

                        <xsl:variable name="productType" select="'ProductType'"/>
                         <xsl:variable name="theSet1" select="boolean(@name = $productType)"/>
                        <xsl:if test="$theSet1">
                        <ProductType><xsl:value-of select="text" /></ProductType>
                        </xsl:if>  

                        <xsl:variable name="BranchNum" select="'BranchNum'"/>
                         <xsl:variable name="theSet2" select="boolean(@name = $BranchNum)"/>
                        <xsl:if test="$theSet2">
                        <BranchNum><xsl:value-of select="text" /></BranchNum>
                        </xsl:if>  

                        <xsl:variable name="CustomeName" select="'CustomeName'"/>
                         <xsl:variable name="theSet3" select="boolean(@name = $CustomeName)"/>
                        <xsl:if test="$theSet3">
                        <CustomeName><xsl:value-of select="text" /></CustomeName>
                        </xsl:if>    


                </xsl:for-each>
            </xsl:element>
    </xsl:for-each>
</xsl:template>