|
Subject: [xsl] Grouping help From: "rick@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Fri, 22 Jul 2022 17:15:21 -0000 |
Hi All,
I have a flat file that I am trying to add some hierarchy to. Here is an
example of the input:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item level="2" number="A.">General</item>
<item level="3" number="(1)">This task gives...</item>
<item level="4" number="(a)">Replace the water...</item>
<item level="4" number="(b)">Replace the barrier...</item>
<item level="4" number="(c)">Replace the...</item>
<item level="3" number="(2)">This task takes...</item>
<note type="note" position="1">NOTE:</note>
<note type="note" position="2">Refer to typical...</note>
<item level="2" number="B.">References</item>
<item level="2" number="C.">Tools/Equipment</item>
<note type="note" position="1">NOTE:</note>
<note type="note" position="2">When more than one...</note>
<effectivity>Effective on:</effectivity>
<effectivity>Effective on:</effectivity>
<item level="2" number="D.">Consumable Materials</item>
<item level="2" number="E.">Location Zones</item>
<item level="2" number="F.">Procedure</item>
<item level="3" number="(1)">Do a check...</item>
<item level="3" number="(2)">Repair or replace...</item>
<item level="3" number="(3)">Do this task...</item>
<item level="3" number="(4)">Do a check...</item>
<item level="3" number="(5)">Replace the sealant...</item>
<item level="3" number="(6)">Do a check of...</item>
<item level="4" number="(a)">Replace the water...</item>
<item level="4" number="(b)">Replace the barrier...</item>
<item level="4" number="(c)">Replace the...</item>
<item level="3" number="(7)">Assemble the...</item>
<item level="3" number="(8)">Seal all of the...</item>
<note type="caution" position="1">CAUTION:</note>
<note type="caution" position="2">MAKE SURE...</note>
<item level="3" number="(9)">Apply a layer of grease as follows:
<para>Apply a layer of...</para></item>
<note type="note" position="1">NOTE:</note>
<note type="note" position="2">The grease can...</note>
<item level="3" number="(10)">Use the <b>vacuum cups</b> ...</item>
</root>
My output is not quite right:
<?xml version="1.0" encoding="UTF-8"?>
<task>
<pretopic lbl="A.">
<title>General</title>
<list1>
<l1item lbl="(1)">This task gives...</l1item>
<list2>
<l2item lbl="(a)">Replace the water...</l2item>
</list2>
</list1>
</pretopic>
<pretopic lbl="B.">
<title>References</title>
</pretopic>
<pretopic lbl="C.">
<title>Tools/Equipment</title>
<note type="note" position="1">NOTE:</note>
<note type="note" position="2">When more than one...</note>
<effectivity>Effective on:</effectivity>
<effectivity>Effective on:</effectivity>
</pretopic>
<pretopic lbl="D.">
<title>Consumable Materials</title>
</pretopic>
<pretopic lbl="E.">
<title>Location Zones</title>
</pretopic>
<pretopic lbl="F.">
<title>Procedure</title>
<list1>
<l1item lbl="(1)">Do a check...</l1item>
<l1item lbl="(2)">Repair or replace...</l1item>
<l1item lbl="(3)">Do this task...</l1item>
<l1item lbl="(4)">Do a check...</l1item>
<l1item lbl="(5)">Replace the sealant...</l1item>
<l1item lbl="(6)">Do a check of...</l1item>
<list2>
<l2item lbl="(a)">Replace the water...</l2item>
</list2>
</list1>
</pretopic>
</task>
Here is my current stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="3.0" expand-text="yes">
<xsl:output indent="yes"/>
<xsl:template match="/root">
<task>
<xsl:for-each-group select="*"
group-starting-with="item[@level='2']">
<xsl:apply-templates select="." mode="group"/>
</xsl:for-each-group>
</task>
</xsl:template>
<xsl:template match="item[@level='2']" mode="group">
<pretopic lbl="{@number}">
<title>{.}</title>
<xsl:for-each-group select="current-group() except ."
group-starting-with="item[@level='3'][@number='(1)']">
<xsl:choose>
<xsl:when test="self::item[@number='(1)']">
<list1>
<xsl:apply-templates select="." mode="group"/>
</list1>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="." mode="group"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</pretopic>
</xsl:template>
<xsl:template match="item[@level='3']" mode="group">
<l1item lbl="{@number}">{.}</l1item>
<xsl:for-each-group select="current-group() except ."
group-starting-with="item[@level='4'][@number='(a)']">
<xsl:choose>
<xsl:when test="self::item[@number='(a)']">
<list2>
<xsl:apply-templates select="." mode="group"/>
</list2>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="." mode="group"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="item[@level='4']" mode="group">
<l2item lbl="{@number}">{.}</l2item>
</xsl:template>
<xsl:template match="*" mode="group">
<xsl:copy-of select="current-group()"/>
</xsl:template>
</xsl:stylesheet>
This example is a modified version of Mike's XHTML version on page 340 of
his book. Any help would be appreciated. Thank you.
Rick
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] XPath expression that ret, Michael Kay mike@xxx | Thread | Re: [xsl] Grouping help, Martin Honnen martin |
| Re: [xsl] XPath expression that ret, Dimitre Novatchev dn | Date | Re: [xsl] XPath expression that ret, Michael Kay mike@xxx |
| Month |