1

我是 XSLT 的新手。我需要使用XSLT将以下输入 xml 格式转换为它下面的所需输出格式(O/P 格式是HTML 中的 unorderedList ) ,以便在 JQuery 插件中使用它。我自己尝试过使用下面的 XSLT 代码,但我需要添加更多内容。我发现很难完成这种转变,任何人都可以帮助我。

输入格式

<Unit id = "2000001">
    <Unit id = "2000002">
        <Unit id = "2000006">
            <Unit id = "2000032">
                <Data>
                    <PartyId>2000032</PartyId>
                    <PartyTypeCode>DEPT</PartyTypeCode>
                    <PartyName>2017964 SM Retirement Party</PartyName>
                </Data>
            </Unit>
            <Unit id = "2000033">
                <Data>
                    <PartyId>2000033</PartyId>
                    <PartyTypeCode>DEPT</PartyTypeCode>
                    <PartyName>2018370 2012 Director's Ornament</PartyName>
                </Data>
            </Unit>
            <Data>
                <PartyId>2000006</PartyId>
                <PartyTypeCode>DEPT</PartyTypeCode>
                <PartyName>Projects Executive</PartyName>
            </Data>
        </Unit>
        <Data>
            <PartyId>2000002</PartyId>
            <PartyTypeCode>SEG</PartyTypeCode>
            <PartyName>Tres Aguilas Management</PartyName>
        </Data>
    </Unit>
    <Data>
        <PartyId>2000001</PartyId>
        <PartyTypeCode>SEG</PartyTypeCode>
        <PartyName>Tres Aguilas Enterprise</PartyName>
    </Data>
</Unit>

输出格式:

<ul>
    <li id = "2000001">
        <span>Tres Aguilas Enterprise</span>
        <ul>
            <li id = "2000002">
                <span>Tres Aguilas Management</span>
                <ul>
                    <li id = "2000006">
                        <span>Projects Executive</span>
                        <ul>
                            <li id = "2000032">
                                <span>2017964 SM Retirement Party</span>                                
                            </li>
                            <li id = "2000033">
                                <span>2018370 2012 Director's Ornament</span>                               
                            </li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

XSLT 代码:

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

<xsl:template match="/">

      <xsl:for-each select="//Unit">
      <ul>
          <li><xsl:value-of select="Data/PartyName"/></li>
      </ul>
      </xsl:for-each>

</xsl:template>
</xsl:stylesheet>
4

2 回答 2

0

:) 非常感谢 Mads Hansen 为我的问题做出了贡献。我终于对您提供的 XSLT 进行了更改,并成功实现了转换为所需格式。这是最终的 XSLT:

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

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

    <!--convert every <Unit> into a <UL>, 
        then "push" the attributes(i.e. @id), 
        and then "push" any <Unit> children-->
    <xsl:template match="Unit">

        <xsl:apply-templates select="@*"/>

    </xsl:template>

    <!--Create an <li> and copy the @id attribute,
        then "push" the Data/PartyName that are children of this <Unit>-->
    <xsl:template match="Unit/@id">

        <li>
            <xsl:copy/>
            <xsl:apply-templates select="../Data/PartyName"/>
            <xsl:if test= "../Unit">
                <ul>
                    <xsl:apply-templates select="../Unit"/>
                </ul>
            </xsl:if>
        </li>

    </xsl:template>

    <!--convert <PartyName> into <span> -->
    <xsl:template match="Data/PartyName">
        <span>
            <xsl:value-of select="."/>
        </span>
    </xsl:template>

</xsl:stylesheet>
于 2013-05-16T09:29:28.227 回答
0

这是一个“推式”样式表,可以实现您想要的。

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

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

    <!--convert every <Unit> into a <UL>, 
        then "push" the attributes(i.e. @id), 
        and then "push" any <Unit> children-->
    <xsl:template match="Unit">
        <ul>
            <xsl:apply-templates select="@*"/>
        </ul>
    </xsl:template>

    <!--Create an <li> and copy the @id attribute,
        then "push" the Data/PartyName that are children of this <Unit>-->
    <xsl:template match="Unit/@id">
        <li>
            <xsl:copy/>
            <xsl:apply-templates select="../Data/PartyName"/>
            <xsl:apply-templates select="../Unit"/>
        </li>
    </xsl:template>

    <!--convert <PartyName> into <span> -->
    <xsl:template match="Data/PartyName">
        <span><xsl:value-of select="."/></span>
    </xsl:template>

</xsl:stylesheet>
于 2013-05-13T13:45:34.880 回答