xml – XSLT拆分结果为3个组

Web应用程序为我提供了一个 XML-feed,我无法改变.我想要做的是将这个XML-feed分成几个无序列表.我正在尝试使用下面的XSLT.
<?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" omit-xml-declaration="yes" indent="yes" encoding="utf-8" />
  <xsl:param name="html-content-type" />
  <xsl:template match="/NavigationTree">
    <xsl:if test="count(//Page) > 0">
      <ul>
        <xsl:apply-templates select="Page">
        </xsl:apply-templates>
      </ul>
    </xsl:if>
  </xsl:template>

  <xsl:template match="//Page">    
    <li class="{position() mod 3}">
      <xsl:text disable-output-escaping="yes"><![CDATA[»&nbsp;]]></xsl:text>
      <a>
        <xsl:attribute name="href">
          <xsl:value-of select="@FriendlyHref" disable-output-escaping="yes"/>
        </xsl:attribute>
        <xsl:value-of select="@MenuText" disable-output-escaping="no"/>
      </a>
    </li>

    <xsl:if test="position() mod 3 = 0">
      <xsl:if test="position() &lt; count(//Page)">
        <!--Don't know if this is the correct approach,but when the position is 3 and there are more items following
        I want to create an new unordered list-->
      </xsl:if>      
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

使用上面的XSLT,我可以将XML转换为无序列表,其中包含6个项目(假设总共有6个项目).类似于下面的例子;

<ul>
    <li>Item1</li>
    <li>Item2</li>
    <li>Item3</li>
    <li>Item4</li>
    <li>Item5</li>
    <li>Item6</li>
</ul>

上面的例子是我现在得到的结果.但是期望的结果是这样的;

<ul>
    <li>Item1</li>
    <li>Item2</li>
    <li>Item3</li>
</ul>
<ul>
    <li>Item4</li>
    <li>Item5</li>
    <li>Item6</li>
</ul>

编辑 – 示例XML输入

<NavigationTree>
    <Settings>
        <!--Snipped data-->
    </Settings>
    <Page ID="5" AreaID="1" MenuText="Bestellen" MouseOver="" Href="Default.aspx?ID=5" FriendlyHref="/nl-nl/klantenservice/bestellen.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" Sort="1" LastInLevel="False" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"/>
    <Page ID="6" AreaID="1" MenuText="Betalen" MouseOver="" Href="Default.aspx?ID=6" FriendlyHref="/nl-nl/klantenservice/betalen.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" Sort="2" LastInLevel="False" InPath="True" ChildCount="0" class="L2_Active" Active="True" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"/>
    <Page ID="7" AreaID="1" MenuText="Retourneren" MouseOver="" Href="Default.aspx?ID=7" FriendlyHref="/nl-nl/klantenservice/retourneren.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" Sort="3" LastInLevel="False" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"/>
    <Page ID="8" AreaID="1" MenuText="Garantie" MouseOver="" Href="Default.aspx?ID=8" FriendlyHref="/nl-nl/klantenservice/garantie.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" Sort="4" LastInLevel="False" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"/>
    <Page ID="9" AreaID="1" MenuText="Faq" MouseOver="" Href="Default.aspx?ID=9" FriendlyHref="/nl-nl/klantenservice/veel-gestelde-vragen.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" Sort="5" LastInLevel="False" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"/>
    <Page ID="10" AreaID="1" MenuText="Contact" MouseOver="" Href="Default.aspx?ID=10" FriendlyHref="/nl-nl/klantenservice/contact.aspx" Image="" ImageActive="" ImageMouseOver="" Title="" Allowclick="True" ShowInSitemap="True" ShowInLegend="True" AbsoluteLevel="2" RelativeLevel="2" Sort="6" LastInLevel="True" InPath="False" ChildCount="0" class="L2" Active="False" IsPagePasswordProtected="False" IsPageUserProtected="False" CanAccessPasswordProtectedPage="False" CanAccessUserProtectedPage="True"/>
</NavigationTree>

如您所见,我总是希望以3个项目为一组进行输出.使用XSLT可以实现这种html输出吗?如果是,我该怎么做?欢迎任何帮助!

要做到这一点,你需要在Page元素上匹配它是位置1,4,7等…换句话说,position()mod 3等于1.
<xsl:if test="position() mod 3 = 1">

这给出了列表的第一个元素.然后,您可以获得其余的2个元素,就像这样

<xsl:apply-templates select=".|following-sibling::Page[position() &lt; 3]" mode="list"/>

完全放下这个,给出了以下XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" encoding="utf-8"/>
   <xsl:param name="html-content-type"/>
   <xsl:param name="group-size" select="3"/>
   <xsl:template match="/NavigationTree">
      <xsl:if test="count(//Page) &gt; 0">
         <xsl:apply-templates select="Page"/>
      </xsl:if>
   </xsl:template>
   <xsl:template match="Page">
      <xsl:if test="position() mod $group-size = 1">
         <ul>
            <xsl:apply-templates select=".|following-sibling::Page[position() &lt; $group-size]" mode="list"/>
         </ul>
      </xsl:if>
   </xsl:template>
   <xsl:template match="Page" mode="list">
      <li class="{position()}">
         <xsl:text disable-output-escaping="yes"><![CDATA[»&nbsp;]]></xsl:text>
         <a>
            <xsl:attribute name="href">
               <xsl:value-of select="@FriendlyHref" disable-output-escaping="yes"/>
            </xsl:attribute>
            <xsl:value-of select="@MenuText" disable-output-escaping="no"/>
         </a>
      </li>
   </xsl:template>
</xsl:stylesheet>

在输入XML上运行时,应生成以下输出

<ul>
  <li class="1">»&nbsp;<a href="/nl-nl/klantenservice/bestellen.aspx">Bestellen</a></li>
  <li class="2">»&nbsp;<a href="/nl-nl/klantenservice/betalen.aspx">Betalen</a></li>
  <li class="3">»&nbsp;<a href="/nl-nl/klantenservice/retourneren.aspx">Retourneren</a></li>
</ul>
<ul>
  <li class="1">»&nbsp;<a href="/nl-nl/klantenservice/garantie.aspx">Garantie</a></li>
  <li class="2">»&nbsp;<a href="/nl-nl/klantenservice/veel-gestelde-vragen.aspx">Faq</a></li>
   <li class="3">»&nbsp;<a href="/nl-nl/klantenservice/contact.aspx">Contact</a></li>
</ul>

请注意我已经参数化了组大小,例如,您可以轻松地将每个列表更改为4或5个元素.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇