使用XSLT基于父字段的嵌套分组

时间:2014-12-15 21:41:48

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

这是我正在使用的原始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。 < / p>

3 个答案:

答案 0 :(得分:3)

试试这个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>

答案 1 :(得分:3)

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

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> 

答案 2 :(得分:0)

有趣的挑战。

这个xsl获取了一个html文档,这是我认为你想要的。它的格式不是很好。匹配模板启动进程搜索没有父节点的节点。然后它调用该类型的模板。该模板以递归方式调用自身来迭代子类型。

我使用命令行app 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>