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[» ]]></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() < 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() < 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) > 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() < $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[» ]]></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>
<ul> <li class="1">» <a href="/nl-nl/klantenservice/bestellen.aspx">Bestellen</a></li> <li class="2">» <a href="/nl-nl/klantenservice/betalen.aspx">Betalen</a></li> <li class="3">» <a href="/nl-nl/klantenservice/retourneren.aspx">Retourneren</a></li> </ul> <ul> <li class="1">» <a href="/nl-nl/klantenservice/garantie.aspx">Garantie</a></li> <li class="2">» <a href="/nl-nl/klantenservice/veel-gestelde-vragen.aspx">Faq</a></li> <li class="3">» <a href="/nl-nl/klantenservice/contact.aspx">Contact</a></li> </ul>
请注意我已经参数化了组大小,例如,您可以轻松地将每个列表更改为4或5个元素.