需要XSLT转换来获取HTML结构的无序列表

时间:2013-05-13 12:29:42

标签: xslt

我是XSLT的新手。 我需要将下面的输入xml格式转换为所需的输出格式(O / P格式是HTML中的无序列表),使用 XSLT 在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>

2 个答案:

答案 0 :(得分:0)

这是一个“push style”样式表,可以实现您的目标。

<?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>

答案 1 :(得分: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>