Create ordered list : list « XSLT stylesheet « XML






Create ordered list


File: Data.xml

<?xml version="1.0"?>
<manual type="assembly" id="model-rocket">
  <parts-list>
    <part label="A" count="1">part 1</part>
    <part label="B" count="1">part 2</part>
    <part label="F" count="4">part 3</part>
    <part label="N" count="3">part 4</part>
    <part label="C" count="1">part 5</part>
  </parts-list>
  <instructions>
    <step>
      Glue
      <part ref="A" />
      and
      <part ref="B" />
      together.
    </step>
    <step>
      For each
      <part ref="F" />
      , .
    </step>
    <step>
      Connect
      <part ref="C" />
    </step>
  </instructions>
</manual>

File: Transform.xslt

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" encoding="ISO-8859-1" />
  <xsl:template match="manual">
    <html>
      <head>
        <title>Instructions Guide</title>
      </head>
      <body>
        <h1>Instructions Guide</h1>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="parts-list">
    <h2>Parts</h2>
    <dl>
      <xsl:apply-templates />
    </dl>
  </xsl:template>
  <xsl:template match="part[@label]">
    <dt>
      <xsl:value-of select="@label" />
    </dt>
    <dd>
      <xsl:apply-templates />
    </dd>
  </xsl:template>
  <xsl:template match="part[@ref]">
    <xsl:variable name="label">
      <xsl:value-of select="@ref" />
    </xsl:variable>
    <xsl:value-of select="//part[@label=$label]"></xsl:value-of>
    <xsl:text> (Part </xsl:text>
    <xsl:value-of select="@ref" />
    <xsl:text>)</xsl:text>
  </xsl:template>
  <xsl:template match="instructions">
    <h2>Steps</h2>
    <ol>
      <xsl:apply-templates />
    </ol>
  </xsl:template>
  <xsl:template match="step">
    <li>
      <xsl:apply-templates />
    </li>
  </xsl:template>

</xsl:stylesheet>


Output:

<?xml version="1.0" encoding="ISO-8859-1"?><html><head><title>Instructions Guide</title></head><body><h1>Instructions Guide</h1>
  <h2>Parts</h2><dl>
    <dt>A</dt><dd>part 1</dd>
    <dt>B</dt><dd>part 2</dd>
    <dt>F</dt><dd>part 3</dd>
    <dt>N</dt><dd>part 4</dd>
    <dt>C</dt><dd>part 5</dd>
  </dl>
  <h2>Steps</h2><ol>
    <li>
      Glue
      part 1 (Part A)
      and
      part 2 (Part B)
      together.
    </li>
    <li>
      For each
      part 3 (Part F)
      , .
    </li>
    <li>
      Connect
      part 5 (Part C)
    </li>
  </ol>
</body></html>

 








Related examples in the same category

1.Create list in transformation
2.Create index number