【问题标题】:Nested grouping based on parent field using XSLT使用 XSLT 基于父字段的嵌套分组
【发布时间】:2015-02-14 02:48:19
【问题描述】:

这是我正在使用的原始 XML 示例:

<dsQueryResponse>
  <Rows>
    <Row Title="Animal" Parent="" />
    <Row Title="Mammal" Parent="Animal" />
    <Row Title="Lion" Parent="Mammal" />
    <Row Title="Plant" Parent="" />
    <Row Title="Elephant" Parent="Mammal" />
  </Rows>
</dsQueryResponse>

使用 XSLT,如何使输出成为嵌套的 UL,例如:

<ul>
  <li>
    Animal
    <ul>
      <li>
        Mammal
        <ul>
          <li>Elephant</li>
          <li>Lion</li>
        </ul>
      </li>
    </ul>
  </li>
  <li>Plant</li>
</ul>

我对 XSLT 只是“还好”,只能做简单的排序,我知道我可以通过 JavaScript/jQuery 轻松做到这一点,但我宁愿为此使用 XSLT。

【问题讨论】:

    标签: xml xslt xpath xslt-1.0 xslt-grouping


    【解决方案1】:

    试试这个 XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="/">
        <ul>
          <xsl:apply-templates select="//Row[@Parent = '']"/>
        </ul>
      </xsl:template>
    
      <xsl:template match="Row">
        <li>
          <xsl:value-of select="@Title"/>
    
          <xsl:if test="../Row[@Parent = current()/@Title]">
            <ul>
              <xsl:apply-templates select="../Row[@Parent = current()/@Title]"/>
            </ul>
          </xsl:if>
        </li>
      </xsl:template>
    </xsl:stylesheet>
    

    输出:

    <ul>
      <li>
        Animal<ul>
          <li>
            Mammal<ul>
              <li>Lion</li>
              <li>Elephant</li>
            </ul>
          </li>
        </ul>
      </li>
      <li>Plant</li>
    </ul>
    

    【讨论】:

    • 如此优雅的解决方案!
    【解决方案2】:

    这里的“优雅”(高效!)解决方案是使用 key 来检索“相关”记录:

    XSLT 1.0

    <?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"/>
    
    <xsl:key name="row-by-parent" match="Row" use="@Parent" />
    
    <xsl:template match="/">
        <ul>
            <xsl:apply-templates select="dsQueryResponse/Rows/Row[not(string(@Parent))]"/>
        </ul>
    </xsl:template>
    
    <xsl:template match="Row">
        <li>
            <xsl:value-of select="@Title"/>
            <xsl:variable name="child-rows" select="key('row-by-parent', @Title)" />
            <xsl:if test="$child-rows">
                <ul>
                    <xsl:apply-templates select="$child-rows"/>
                </ul>
            </xsl:if>
        </li>
    </xsl:template>
    
    </xsl:stylesheet> 
    

    【讨论】:

      【解决方案3】:

      有趣的挑战。

      这个 xsl 得到一个我认为你想要的 html 文档。虽然它的格式不漂亮。匹配的模板开始搜索没有父节点的过程。然后它调用该类型的模板。该模板递归调用自身来迭代子类型。

      我使用命令行应用程序 msxsl 对其进行了测试。 YMMV

      <?xml version="1.0" encoding="utf-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                      exclude-result-prefixes="msxsl" version="1.0"
                      >
        <xsl:output omit-xml-declaration="yes" method="xml" indent="yes"/>
      
        <xsl:template name="CategoryTemplate">
          <xsl:param name="CategoryName"/>
      
          <ul>
            <xsl:for-each select="/*[local-name()='dsQueryResponse']/*[local-name()='Rows']/*[local-name()='Row' and @Parent=$CategoryName]">
              <li>
                <xsl:value-of select="@Title"/>
      
                <!--recursively call template with category-->
                <xsl:call-template name="CategoryTemplate">
                  <xsl:with-param name="CategoryName" select="@Title"/>
                </xsl:call-template>
              </li>
            </xsl:for-each>
          </ul>
      
        </xsl:template>
      
        <xsl:template match="/*[local-name()='dsQueryResponse']/*[local-name()='Rows']">
      
          <xsl:variable name="CategoryName">
            <xsl:text></xsl:text>
          </xsl:variable>
      
          <html>
            <body>
              <xsl:call-template name="CategoryTemplate">
                <xsl:with-param name="CategoryName" select="$CategoryName"/>
              </xsl:call-template>
            </body>
          </html>
        </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

      • XSLT 有一个 built-in recursion mechanismxsl:apply-templates 指令。没有必要如此努力地复制它。
      • 这个有标签没有在正确的地方关闭的问题。使用xslttest.appspot.com 测试
      • 有趣的是它在 msxsl 命令行应用程序中运行,标签未关闭
      • @michael.hor257k 的解决方案似乎比这个好得多。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2015-04-10
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      相关资源
      最近更新 更多