Re: [xsl] Dirty Input

Subject: Re: [xsl] Dirty Input
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 20 Jul 2006 12:11:32 +0100
> The tag <Devices> contains elements <a>, <b> and <c> in that order
It's good not to confuse "tag" with "element", the tag <Devices> just
contains the element name "Devices", being a start tag it may also have
contained some attributes, but start tags never contain elements.

> Input file:
It's helpful if you make the example input well formed (I added a top
level element <x>...</x> around it all so that it could be passed as
input to xsl)



This sounds like a standard grouping problem, see Jeni's site for xslt1
solutions or in xslt2 your description is exactly the description of
group-starting-with, so you just need something like:



<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  <xsl:template match="*">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="Devices">
    <xsl:for-each-group select="*" group-starting-with="a">
      <device>
	<xsl:apply-templates select="current-group()"/>
      </device>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>


which produces


$ saxon8 grp.xml grp.xsl
<?xml version="1.0" encoding="UTF-8"?>
<x>
   <bla>x</bla>
   <bla>x</bla>
    <device>
      <a>
         <a2>1</a2>
      </a>
      <b>
         <b2>
            <b3>1</b3>
            <b4>1</b4>
         </b2>
         <b2i>
            <b3i>1</b3i>
            <b4i>1</b4i>
         </b2i>
                ...
        </b>
      <c>1</c>
   </device>
   <device>
      <a>
         <a2>1</a2>
      </a>
      <b>
         <b2>
            <b3>1</b3>
            <b4>1</b4>
         </b2>
         <b2i>
            <b3i>1</b3i>
            <b4i>1</b4i>
         </b2i>
                ...
        </b>
      <c>1</c>
   </device>
</x>

Current Thread