Re: [xsl] Deepening a flat structure and numbering nodes

Subject: Re: [xsl] Deepening a flat structure and numbering nodes
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sat, 10 Aug 2013 12:06:19 -0400
At 2013-08-10 08:53 -0400, Rick Quatro wrote:
I need to do something different to the
<DifferentialDiagnosis> elements. I want to change the element names by
numbering them sequentially within my <newRecord> element, like this:

<?xml version="1.0" encoding="UTF-8"?>
<data>
   <newRecord>
      <Category>Category</Category>
      <Subcategory>Subcategory</Subcategory>
      <Case>1</Case>
      <CaseTitle>Title One</CaseTitle>
      <Institution>Institution One</Institution>
      <Author>Authors One</Author>
      <History>History One</History>
      <DifferentialDiagnosis1>Sick</DifferentialDiagnosis1>
      <DifferentialDiagnosis2>Sicker</DifferentialDiagnosis2>
      <DifferentialDiagnosis3>Sickest</DifferentialDiagnosis3>
      <TeachingPoint>Point1</TeachingPoint>
      <TeachingPoint>Point2</TeachingPoint>
   </newRecord>
   <newRecord>
      <Category>Category One</Category>
      <Subcategory>Subcategory</Subcategory>
      <Case>2</Case>
      <CaseTitle>Title Two</CaseTitle>
      <Institution>Title Two</Institution>
      <Author>Author Two</Author>
      <History>History Two</History>
      <DifferentialDiagnosis1>Sick</DifferentialDiagnosis1>
      <DifferentialDiagnosis2>Sicker</DifferentialDiagnosis2>
      <DifferentialDiagnosis3>Sickest</DifferentialDiagnosis3>
<TeachingPoint>Point1</TeachingPoint>
      <TeachingPoint>Point2</TeachingPoint>
   </newRecord>
</data>

I added this rule to my stylesheet:

    <xsl:template match="DifferentialDiagnosis" mode="in-case-siblings">
        <xsl:variable name="diagnosis"><xsl:number
count="DifferentialDiagnosis" from="Category"/></xsl:variable>
        <xsl:element name="{concat(name(),$diagnosis)}"><xsl:value-of
select="."/></xsl:element>
        <xsl:apply-templates
            select="following-sibling::*[1][not(self::Category)]"
mode="in-case-siblings"/>
    </xsl:template>

This basically "works" except the numbers are sequential throughout the
document.

<data>
   <newRecord>
        ...
      <DifferentialDiagnosis1>Sick</DifferentialDiagnosis1>
      <DifferentialDiagnosis2>Sicker</DifferentialDiagnosis2>
      <DifferentialDiagnosis3>Sickest</DifferentialDiagnosis3>
        ...
   </newRecord>
   <newRecord>
        ...
      <DifferentialDiagnosis1>Sick</DifferentialDiagnosis5>
      <DifferentialDiagnosis2>Sicker</DifferentialDiagnosis6>
      <DifferentialDiagnosis3>Sickest</DifferentialDiagnosis7>
        ...
   </newRecord>
</data>

I need the numbers to reset for each <newRecord> element. I am sorry for the
long post, but I want to be complete as possible. Also, for this project, I
need to use XSLT 1.0.

Just count the difference of those from where you are less those that are before the closest group before:


<xsl:element name="concat(name(.),
1 + count(preceding-sibling::DifferentialDiagnosis)
- count(preceding-sibling::Category[1]/preceding-sibling::DifferentialDiagnosis))">


I hope this helps.

. . . . . . . . Ken

--
Public XSLT, XSL-FO, and UBL classes in the Netherlands     Oct 2013 |
Public XSLT, XSL-FO, UBL and code list classes in Australia Oct 2013 |
Contact us for world-wide XML consulting and instructor-led training |
Free 5-hour lecture: http://www.CraneSoftwrights.com/links/udemy.htm |
Crane Softwrights Ltd.            http://www.CraneSoftwrights.com/s/ |
G. Ken Holman                   mailto:gkholman@xxxxxxxxxxxxxxxxxxxx |
Google+ profile: https://plus.google.com/116832879756988317389/about |
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal |

Current Thread