Re: [xsl] nesting flat varaible structures?

Subject: Re: [xsl] nesting flat varaible structures?
From: Dan Vint <dvint@xxxxxxxxx>
Date: Mon, 26 Sep 2011 18:58:18 -0700
This worked great, I had to change it up a little bit because the attributes needed to be renamed, but otherwise good. XSLT sure is getting more powerful and with unique techniques/tricks that are not available in other languages. I had used this functionality before but with nicely nested constructs.

..dan

At 01:39 PM 9/23/2011, you wrote:
At 2011-09-23 12:25 -0700, dvint@xxxxxxxxx wrote:
I'm trying to use XSLT to do a conversion from one version of the S1000D
spec to another. So in the older version you can have this:

<reqpers>
<person man="1"/>
<perscat category="tech"/>
<trade>foo</trade>
<person man="2"/>
<perscat category="techa"/>
<perskill skill="sk05"/>
<trade>bar</trade>
<esttime>23</esttime>
</reqpers>

In the newer version you have a nested structure:

<reqPersons>
<person man="1">
  <personCategory personCategoryCode="tech"/>
  <trade>foo</trade>
</person>
<person man="2">
  <personCategory personCategoryCode="techa"/>
  <personSkill skillLevelCode="sk05"/>
  <trade>bar</trade>
  <esttime>23</esttime>
</person>
<reqPersons>

I can use the following sibling to pull the elements under a new <person>
tag, but the problem is that none of the following elements are requried.

Exactly, which is why you have to look down from the top at the elements as groups, rather than work from each person. Grouping the content as siblings gives you the scope for each person.


I'm working in 2.0. Any ideas on how to correct the results?

I hope the code below helps. Remember that group-starting-with= is a match pattern (not a select expression), and that <xsl:for-each-group> can be considered to be read as <xsl:for-the-first-member-of-each-group> (which my students have found helpful).


. . . . . . . Ken

~/t/ftemp $ cat dan.xml
<?xml version="1.0" encoding="UTF-8"?>
<reqpers>
<person man="1"/>
<perscat category="tech"/>
<trade>foo</trade>
<person man="2"/>
<perscat category="techa"/>
<perskill skill="sk05"/>
<trade>bar</trade>
<esttime>23</esttime>
</reqpers>
~/t/ftemp $ xslt2 dan.xml dan.xsl
<?xml version="1.0" encoding="UTF-8"?>
<reqPersons>
   <person man="1">
      <perscat category="tech"/>
      <trade>foo</trade>
   </person>
   <person man="2">
      <perscat category="techa"/>
      <perskill skill="sk05"/>
      <trade>bar</trade>
      <esttime>23</esttime>
   </person>
</reqPersons>~/t/ftemp $
~/t/ftemp $ cat dan.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="reqpers">
  <reqPersons>
    <!--the following assumes <person> is the first child-->
    <xsl:for-each-group select="*" group-starting-with="person">
      <!--preserve the element-->
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <!--move sibling elements in the group underneath the first-->
        <xsl:copy-of select="current-group()[position()>1]"/>
      </xsl:copy>
    </xsl:for-each-group>
  </reqPersons>
</xsl:template>

</xsl:stylesheet>
~/t/ftemp $


-- Contact us for world-wide XML consulting and instructor-led training 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


--------------------------------------------------------------------------- Danny Vint

Panoramic Photography
http://www.dvint.com

voice: 619-938-3610

Current Thread