Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML/XSLT Displaying Nested Lists 1

Status
Not open for further replies.

pcrogers

Programmer
Jan 28, 2004
2
CA
Hello all,

I am working with an xml document that has the following basic structure:
<action-list>
<action>
<title>Title</title>
<bullet level=&quot;1&quot;>First item</bullet>
<bullet level=&quot;1&quot;>Second item</bullet>
<bullet level=&quot;2&quot;>First subitem</bullet>
<bullet level=&quot;2&quot;>Second subitem</bullet>
<bullet level=&quot;1&quot;>Third item</bullet>
</action>
</action-list>

The type of output I am looking for is
Title
1. First Item
2. Second Item
a. First subitem
b. Second subitem
3. Third Item

I am not having much luck coming up with the code that allows for this to happen.
Thanks Phil
 
It must be possible, going from node to node using named templates, but I think it's very tricky.
The main problem is to find out what to do with:
<bullet level=&quot;2&quot;>Onehundredthird item</bullet>
To what level-1 does it belong, how many level-2 nodes that belong to the same level-1 node before this one?

I guess you better try te get a nicer xml to start with:
<action-list>
<action>
<title>Title</title>
<bullet level=&quot;1&quot; text=&quot;First item&quot;/>
<bullet level=&quot;1&quot; text=&quot;Second item&quot;>
<bullet level=&quot;2&quot; text=&quot;First subitem&quot;/>
<bullet level=&quot;2&quot; text=&quot;Second subitem&quot;/>
</bullet>
</action>
</action-list>
 
Well, just for fun:
Your XML can be parsed with:
Code:
  <xsl:template match=&quot;action&quot;>
    <xsl:value-of select=&quot;title&quot;/>
    <xsl:call-template name=&quot;MakeBullet&quot;>
      <xsl:with-param name=&quot;thisnode&quot; select=&quot;bullet&quot;/>
      <xsl:with-param name=&quot;levelno1&quot; select=&quot;0&quot;/>
      <xsl:with-param name=&quot;levelno2&quot; select=&quot;0&quot;/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name=&quot;MakeBullet&quot;>
    <xsl:param name=&quot;thisnode&quot;/>
    <xsl:param name=&quot;levelno1&quot;/>
    <xsl:param name=&quot;levelno2&quot;/>
    <xsl:variable name=&quot;mylevelno1&quot; select=&quot;$levelno1+2-$thisnode/@level&quot;/>
    <xsl:variable name=&quot;mylevelno2&quot; select=&quot;$levelno2 -1 +$thisnode/@level&quot;/>
    <table><tr>
    <xsl:choose>
      <xsl:when test=&quot;number($thisnode/@level)='2'&quot;>
        <td width=&quot;5%&quot;/>
        <td width=&quot;5%&quot;>
          <xsl:call-template name=&quot;numbertochar&quot;>
            <xsl:with-param name=&quot;number&quot; select=&quot;$mylevelno2&quot;/>
          </xsl:call-template>
        </td>
      </xsl:when>
      <xsl:otherwise>
        <td width=&quot;5%&quot;>
          <xsl:value-of select=&quot;$mylevelno1&quot;/>
        </td>
      </xsl:otherwise>
    </xsl:choose>
    <td>
      <xsl:value-of select=&quot;$thisnode&quot;/>
    </td>
    </tr></table>
    <xsl:if test=&quot;$thisnode/following-sibling::bullet&quot;>
      <xsl:call-template name=&quot;MakeBullet&quot;>
        <xsl:with-param name=&quot;thisnode&quot; select=&quot;$thisnode/following-sibling::bullet&quot;/>
        <xsl:with-param name=&quot;levelno1&quot; select=&quot;$mylevelno1&quot;/>
        <xsl:with-param name=&quot;levelno2&quot;>
          <xsl:choose>
            <xsl:when test=&quot;number($thisnode/@level)='1'&quot;>
              <xsl:value-of select=&quot;0&quot;/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select=&quot;$mylevelno2&quot;/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name=&quot;numbertochar&quot;>
    <xsl:param name=&quot;number&quot;/>
    <xsl:choose>
      <xsl:when test=&quot;$number=1&quot;>
        <xsl:value-of select=&quot;'a'&quot;/>
      </xsl:when>
      <xsl:when test=&quot;$number=2&quot;>
        <xsl:value-of select=&quot;'b'&quot;/>
      </xsl:when>
      <xsl:when test=&quot;$number=3&quot;>
        <xsl:value-of select=&quot;'c'&quot;/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
But I would prefer the xml like I posted (I changed it a liitle):
Code:
<action-list>
  <action title=&quot;Title&quot;>
    <bullet level=&quot;1&quot; text=&quot;First item&quot;/>
    <bullet level=&quot;1&quot; text=&quot;Second item&quot;>
      <bullet level=&quot;2&quot; text=&quot;First subitem&quot;/>
      <bullet level=&quot;2&quot; text=&quot;Second subitem&quot;/>
    </bullet>
    <bullet level=&quot;1&quot; text=&quot;Third item&quot;>
      <bullet level=&quot;2&quot; text=&quot;Third subitem&quot;/>
      <bullet level=&quot;2&quot; text=&quot;Fourth subitem&quot;/>
    </bullet>
  </action>
</action-list>

  <xsl:template match=&quot;action&quot;>
    <xsl:value-of select=&quot;@title&quot;/>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match=&quot;bullet&quot;>
    <table><tr>
      <xsl:choose>
        <xsl:when test=&quot;number(@level)='2'&quot;>
          <td width=&quot;5%&quot;/><td width=&quot;5%&quot;>
            <xsl:call-template name=&quot;numbertochar&quot;>
              <xsl:with-param name=&quot;number&quot; select=&quot;position()&quot;/>
            </xsl:call-template>
          </td>
        </xsl:when>
        <xsl:otherwise>
          <td width=&quot;5%&quot;>
            <xsl:value-of select=&quot;position()&quot;/>
          </td>
        </xsl:otherwise>
      </xsl:choose>
      <td>
        <xsl:value-of select=&quot;@text&quot;/>
      </td>
    </tr></table>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template name=&quot;numbertochar&quot;>
    <xsl:param name=&quot;number&quot;/>
    <xsl:choose>
      <xsl:when test=&quot;$number=1&quot;>
        <xsl:value-of select=&quot;'a'&quot;/>
      </xsl:when>
      <xsl:when test=&quot;$number=2&quot;>
        <xsl:value-of select=&quot;'b'&quot;/>
      </xsl:when>
      <xsl:when test=&quot;$number=3&quot;>
        <xsl:value-of select=&quot;'c'&quot;/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top