[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Help on XSL to tranform simple flat xml sequential

Subject: Re: Help on XSL to tranform simple flat xml sequentially
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Sun, 30 Aug 2009 18:17:02 +0200
Re:  Help on XSL to tranform simple flat xml sequential
Christian Oshiro wrote:
This seems like a pretty simple thing to do, but I usually rely on nested xml. How can I tranform xml that looks like this:

<para role=title>main title1</para>
<para role=subtitle>subtitle1</para>
<para>This is the body of the paragraph. 1</para>
<list>some stuff 1</list>
<para role=title>main title2</para>
<para role=subtitle>subtitle2</para>
<para>This is the body of the paragraph. 2</para>
<list>some stuff 2</list>
<para role=title>main title3</para>
<para role=subtitle>subtitle3</para>
<para>This is the body of the paragraph. 3</para>
<list>some stuff 3</list>

I'd like to transform this using xlst to something like this:

<paragraph>
  <title>main title1</title>

<subtitle>subtitle1</subtitle>

<body>This is the body of the paragraph. 1</body>

<list>some stuff 1</list>
</paragraph>
<paragraph>
  <title>main title2</title>
  <subtitle>subtitle2</subtitle>
  <body>This is the body of the paragraph. 2</body>
  <list>some stuff 2</list>
</paragraph>
<paragraph>
  <title>main title3</title>
  <subtitle>subtitle3</subtitle>
  <body>This is the body of the paragraph. 3</body>
  <list>some stuff 3</list>
</paragraph>

So i need to go node by node and test when role=title shows up. Thanks for the insight.

Here is an XSLT 2.0 stylesheet:



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

<xsl:output indent="yes"/>

<xsl:template match="doc">
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="para[@role = 'title']">
<paragraph>
<xsl:apply-templates select="current-group()"/>
</paragraph>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>


  <xsl:template match="para[@role = 'title']">
    <title><xsl:apply-templates/></title>
  </xsl:template>

  <xsl:template match="para[@role = 'subtitle']">
    <subtitle><xsl:apply-templates/></subtitle>
  </xsl:template>

  <xsl:template match="para[not(@role)]">
    <body><xsl:apply-templates/></body>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

--

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.