Note that there are some explanatory texts on larger screens.

plurals
  1. POXSLT templates and recursion
    text
    copied!<p>Im new to XSLT and am having some problems trying to format an XML document which has recursive nodes.</p> <p>My XML Code:</p> <p>Hopefully my XML shows:</p> <ul> <li>All <code>&lt;item&gt;</code> are nested with <code>&lt;items&gt;</code></li> <li>An item can have either just attributes, or sub nodes</li> <li>The level to which <code>&lt;item&gt;</code> nodes are nested can be infinently deep</li> </ul> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; - &lt;items&gt; &lt;item groupID="1" name="Home" url="//" /&gt; - &lt;item groupID="2" name="Guides" url="/Guides/"&gt; - &lt;items&gt; - &lt;item groupID="26" name="Online-Poker-Guide" url="/Guides/Online-Poker-Guide/"&gt; - &lt;items&gt; - &lt;item&gt; &lt;id&gt;107&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Poker Betting - Online Poker Betting Structures ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/online-poker-betting-structures ]]&gt; &lt;/url&gt; &lt;/item&gt; - &lt;item&gt; &lt;id&gt;114&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Beginners&amp;#39; Poker - Poker Hand Ranking ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/online-poker-hand-ranking ]]&gt; &lt;/url&gt; &lt;/item&gt; - &lt;item&gt; &lt;id&gt;115&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Poker Terms - 4th Street and 5th Street ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/online-poker-poker-terms ]]&gt; &lt;/url&gt; &lt;/item&gt; - &lt;item&gt; &lt;id&gt;116&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Popular Poker - The Popularity of Texas Hold&amp;#39;em ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/online-poker-popularity-texas-holdem ]]&gt; &lt;/url&gt; &lt;/item&gt; - &lt;item&gt; &lt;id&gt;364&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ The Impact of Traditional Poker on Online Poker (and vice versa) ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/online-poker-tradional-vs-online ]]&gt; &lt;/url&gt; &lt;/item&gt; - &lt;item&gt; &lt;id&gt;365&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ The Ultimate, Absolute Online Poker Scandal ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/online-poker-scandal ]]&gt; &lt;/url&gt; &lt;/item&gt; &lt;/items&gt; - &lt;items&gt; - &lt;item groupID="27" name="Beginners-Poker" url="/Guides/Online-Poker-Guide/Beginners-Poker/"&gt; - &lt;items&gt; + &lt;item&gt; &lt;id&gt;101&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Poker Betting - All-in On the Flop ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/poker-betting-all-in-on-the-flop ]]&gt; &lt;/url&gt; &lt;/item&gt; + &lt;item&gt; &lt;id&gt;102&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Beginners&amp;#39; Poker - Choosing an Online Poker Room ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/beginners-poker-choosing-a-room ]]&gt; &lt;/url&gt; &lt;/item&gt; + &lt;item&gt; &lt;id&gt;105&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Beginners&amp;#39; Poker - Choosing What Type of Poker to Play ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/beginners-poker-choosing-type-to-play ]]&gt; &lt;/url&gt; &lt;/item&gt; + &lt;item&gt; &lt;id&gt;106&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Online Poker - Different Types of Online Poker ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/online-poker ]]&gt; &lt;/url&gt; &lt;/item&gt; + &lt;item&gt; &lt;id&gt;109&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Online Poker - Opening an Account at an Online Poker Site ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/online-poker-opening-an-account ]]&gt; &lt;/url&gt; &lt;/item&gt; + &lt;item&gt; &lt;id&gt;111&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Beginners&amp;#39; Poker - Poker Glossary ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/beginners-poker-glossary ]]&gt; &lt;/url&gt; &lt;/item&gt; + &lt;item&gt; &lt;id&gt;117&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Poker Betting - What is a Blind? ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/poker-betting-what-is-a-blind ]]&gt; &lt;/url&gt; &lt;/item&gt; - &lt;item&gt; &lt;id&gt;118&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Poker Betting - What is an Ante? ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/poker-betting-what-is-an-ante ]]&gt; &lt;/url&gt; &lt;/item&gt; + &lt;item&gt; &lt;id&gt;119&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Beginners Poker - What is Bluffing? ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/online-poker-what-is-bluffing ]]&gt; &lt;/url&gt; &lt;/item&gt; - &lt;item&gt; &lt;id&gt;120&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Poker Games - What is Community Card Poker? ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/online-poker-what-is-community-card-poker ]]&gt; &lt;/url&gt; &lt;/item&gt; - &lt;item&gt; &lt;id&gt;121&lt;/id&gt; - &lt;title&gt; - &lt;![CDATA[ Online Poker - What is Online Poker? ]]&gt; &lt;/title&gt; - &lt;url&gt; - &lt;![CDATA[ /Guides/Online-Poker-Guide/Beginners-Poker/online-poker-what-is-online-poker ]]&gt; &lt;/url&gt; &lt;/item&gt; &lt;/items&gt; &lt;/item&gt; &lt;/items&gt; &lt;/item&gt; &lt;/items&gt; &lt;/item&gt; &lt;/items&gt; </code></pre> <p>The XSL code:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="html" indent="yes"/&gt; &lt;xsl:template name="loop"&gt; &lt;xsl:for-each select="items/item"&gt; &lt;ul&gt; &lt;li&gt;&lt;xsl:value-of select="@name" /&gt;&lt;/li&gt; &lt;xsl:if test="@name and child::node()"&gt; &lt;ul&gt; &lt;xsl:for-each select="items/item"&gt; &lt;li&gt;&lt;xsl:value-of select="@name" /&gt;test&lt;/li&gt; &lt;/xsl:for-each&gt; &lt;/ul&gt; &lt;xsl:call-template name="loop" /&gt; &lt;/xsl:if&gt; &lt;xsl:if test="child::node() and not(@name)"&gt; &lt;xsl:for-each select="/items"&gt; &lt;li&gt;&lt;xsl:value-of select="id" /&gt;&lt;/li&gt; &lt;/xsl:for-each&gt; &lt;/xsl:if&gt; &lt;/ul&gt; &lt;/xsl:for-each&gt; &lt;xsl:for-each select="item/items/item"&gt; &lt;li&gt;hi&lt;/li&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;xsl:template match="/" name="test"&gt; &lt;xsl:call-template name="loop" /&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Im trying to write the XSL so that every <code>&lt;items&gt;</code> node will render a <code>&lt;ul&gt;</code> and every <code>&lt;items&gt;</code> node will render an <code>&lt;li&gt;</code>.</p> <p>The XSL needs to be recursive because i cant tell how deep the nested nodes will go.</p> <p>Can anyone help?</p> <p>Regards, Al</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload