0
votes

I want to iterate with xsl over all "items" in "Cars".

If an "item" in "Cars" has an element "Values" I want to display all values of that list "CARS" which are defined under "Lists" again as "item" with listID="CARS".

So here it would be 0 and 1.

Can this be done with xslt?

<Module>  
   <Lists>
      <item listID="CARS">
         <Description>Features</Description>
         <ElementValue elementID="ACTIVE" value="0">
         <Description></Description>
         </ElementValue>

         <ElementValue elementID="INACTIVE" value="1">
         <Description></Description>
         </ElementValue>
      </item>   
   </Lists>

   <Cars>  
      <item>
         <Name>Bounty</Name>
         <Values listRef="CARS"></Values>
      </item>
   </Cars>  
</Module>    
1

1 Answers

0
votes

For cross-references you can define a key <xsl:key name="by-id" match="Module/Lists/item" use="@listID"/>, then a template

<xsl:template match="Cars/item">
  <xsl:variable name="referenced-items" select="key('by-id', Values/@listRef)"/>
  ...
</xsl:template>

can reference the other items as shown using the key function.

Here is a complete example:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="html" version="5.0" indent="yes" />

    <xsl:key name="by-id" match="Module/Lists/item" use="@listID"/>

    <xsl:template match="Module">
        <div>
            <h1>Module</h1>
            <xsl:apply-templates select="Cars"/>
        </div>
    </xsl:template>

    <xsl:template match="Cars">
        <ul>
            <xsl:apply-templates/>
        </ul>
    </xsl:template>

    <xsl:template match="Cars/item">
        <li>
            <span><xsl:value-of select="Name"/></span>
            <xsl:variable name="referenced-items" select="key('by-id', Values/@listRef)"/>
            <xsl:if test="$referenced-items">
                <ul>
                    <xsl:apply-templates select="$referenced-items/ElementValue/@value"/>
                </ul>
            </xsl:if>
        </li>
    </xsl:template>

    <xsl:template match="Lists/item/ElementValue/@value">
        <li>
            <xsl:value-of select="."/>
        </li>
    </xsl:template>
</xsl:transform>

It is online at http://xsltransform.net/3NzcBu2 and outputs the HTML

<div>
   <h1>Module</h1>
   <ul>  

      <li><span>Bounty</span><ul>
            <li>0</li>
            <li>1</li>
         </ul>
      </li>

   </ul>
</div>

which displays as

Module

  • Bounty
    • 0
    • 1