Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Trouble with XSL

From: "George Bina" <george@---------.--->
To: NULL
Date: 1/3/2007 2:13:00 AM

Hi Andy,

This is known as positional grouping.
In XSLT 2.0 you can have for instance something like

<?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="/test">
    <xsl:for-each-group select="*"
group-starting-with="heading1|heading2">
      <xsl:copy>
        <xsl:for-each select="current-group()[position()>1]">
          <xsl:copy-of select="."/>
        </xsl:for-each>
      </xsl:copy>
    </xsl:for-each-group>
  </xsl:template>
</xsl:stylesheet>

Applied on
<test>
  <heading1>fdfdfdfdf</heading1>
  <para>text1</para>
  <para>text2</para>
  <table>Table structure</table>
  <para>text3</para>
  <heading2>gfhgjg</heading2>
  <para>text4</para>
</test>
will give you
<?xml version="1.0" encoding="UTF-8"?>
<heading1>
   <para>text1</para>
   <para>text2</para>
   <table>Table structure</table>
   <para>text3</para>
</heading1>
<heading2>
   <para>text4</para>
</heading2>

An XSLT 1.0 solution is to walk along following sibling axes until the
next node, like below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output indent="yes"/>
  <xsl:template match="/test">
    <xsl:for-each select="heading1|heading2">
      <xsl:variable name="headerId" select="generate-id(.)"/>
      <xsl:copy>
        <xsl:for-each select="following-sibling::*[not(self::heading2
or self::heading1)
          and generate-id(preceding-sibling::*[self::heading1 or
self::heading2][1])=$headerId]">
          <xsl:copy-of select="."/>
        </xsl:for-each>
      </xsl:copy>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
www.---.com




carra@u... wrote:
> Hi
> If I have some XML that looks like this...
>
> <heading1>fdfdfdfdf</heading>
> <para>text1</para>
> <para>text2</para>
> <table>Table structure</table>
> <para>text3</para>
> <heading2>gfhgjg</heading>
> <para>text4</para>
>
> How do I transform it with XSL so that all the para's and tables under
> the headings are wrapped within the heading so that it looks like
> this....
>
> <heading1>
>    <para>text1</para>
>    <para>text2</para>
>    <table>Table structure</table>
>    <para>text3</para>
> </heading>
> <heading2>
>    <para>text4</para>
> </heading2>
> 
> Any help would be appreciated
> 
> Regards
> Andy



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent