Re: [xsl] [xslt] xslt problem

Subject: Re: [xsl] [xslt] xslt problem
From: Ronan Klyne <ronan.klyne@xxxxxxxxxxx>
Date: Thu, 19 Mar 2009 08:59:56 +0000
Michalmas wrote:
> Hello guys,
> 
> I have following XML:
> 
> <example>
>     <someNode>
>             <value>asas</value>
>             <name>asas</name>
>             <space>12</space>
>     </someNode>
>     <interestingNode>
>             <con>someString</con>
>             <follow>
>                     <node1>1</node1>
>                     <node2>2</node2>
>                     <node3>3</node3>
>                     <node4>4</node4>
>             </follow>
>             <space>5</space>
>     </interestingNode>
> </example>
> 
> 
> Now i want to transform it to:
> 
> <example>
>     <someNode>
>             <value>asas</value>
>             <name>asas</name>
>             <space>12</space>
>     </someNode>
>     <interestingNode>
>             <con>someString</con>
>             <follow>
>                     <return>0</return>
>             </follow>
>             <space>5</space>
>     </interestingNode>
> 
>     <node1 space="5">1</node1>
>     <node2 space="5">2</node2>
>     <node3 space="5">3</node3>
>     <node4 space="5">4</node4>
> </example>
> 
> 
> The changes are:
> - when interestingNode is found, we check the con value
> - if con value conforms someString, then we make transformation of this part by:
>     - evrything in follow node is moved outside interestingNode
>     - follow node gets one child return
>     - all nodes moved outside get an attribute space with value
> specified in original node
> 

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

<xsl:template match="example">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="interestingNode[con='someString']">
  <interestingNode>
    <xsl:copy-of select="con" />
    <follow>
      <return>0</return>
    </follow>
    <xsl:copy-of select="space" />
  </interestingNode>
  <xsl:variable name="space" select="space" />
  <xsl:for-each select="follow/*">
    <xsl:copy>
      <xsl:attribute name="space" select="$space" />
      <xsl:copy-of select="node()" />
    </xsl:copy>
  </xsl:for-each>
</xsl:template>

<xsl:template match="*">
  <xsl:copy-of select="." />
</xsl:template>

</xsl:stylesheet>

This template matches the interestingNode, then generates two blocks of
output - first the slightly altered interestingNode, then the list of
<node> tags.

Hope that helps,

	Ronan

-- 
Ronan Klyne
Business Collaborator Developer
Tel: +44 01189 028518
ronan.klyne@xxxxxxxxxxx
www.groupbc.com

Current Thread