需要XSLT转换以获得类似HTML结构的无序列表

需要XSLT转换以获得类似HTML结构的无序列表,xslt,Xslt,我是XSLT新手。 我需要使用XSLT将下面的输入xml格式转换为它下面所需的输出格式(O/P格式是HTML中的无序列表),以便在JQuery插件中使用它。我自己也尝试过下面的XSLT代码,但我需要添加更多内容。我发现很难完成这一转变,有人能帮我吗 输入格式 2000032 部 2017964 SM退休党 2000033 部 2018370 2012年董事装饰品 2000006 部 项目主管 2000002 赛格 树突管理 2000001 赛格 特雷斯阿吉拉斯企业 输出格式: 特雷斯阿吉

我是XSLT新手。 我需要使用XSLT将下面的输入xml格式转换为它下面所需的输出格式(O/P格式是HTML中的无序列表),以便在JQuery插件中使用它。我自己也尝试过下面的XSLT代码,但我需要添加更多内容。我发现很难完成这一转变,有人能帮我吗

输入格式

2000032
部
2017964 SM退休党
2000033
部
2018370 2012年董事装饰品
2000006
部
项目主管
2000002
赛格
树突管理
2000001
赛格
特雷斯阿吉拉斯企业
输出格式:
  • 特雷斯阿吉拉斯企业
    • 树突管理
      • 项目主管
        • 2017964 SM退休党
        • 2018370 2012年董事装饰品
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">
        <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>

  • :)非常感谢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>
    
    
    

  • Hi Mads Hansen,感谢您的回复,我无法使用此文件获得所需的输出格式,我已尝试修改它,但无法完成。你能帮帮我吗?哎呀!很抱歉。我在
    模板中选择了
    元素,然后留下了应用模板。它应该只将模板应用于属性。在
    @id
    的模板内部,将处理任何
    子项(这确保它们嵌套在
  • 中)。嘿,感谢您的更正。您已经接近了,但唯一的问题是
  • 在输出中,这两个元素都应该在
      内,但由于当前xslt为每个“Unit”元素提供了一个单独的
        。您能纠正这个问题吗?
        <?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>
        
        <?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>
        
        <?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>