Altova Mailing List Archives


Re: [xsl] another placement of nodes question

From: a kusa <akusa8@-----.--->
To: xsl-list@-----.------------.---
Date: 1/25/2010 6:09:00 PM
Hi Martin:

Thank you for your input.The issue I am having here is when I use
level2/item[last()], the processor considers both item elements under
<level2> as last ones. So I still get repeated <spec> elements under
both item elements.
How do I get the position of <item> relative to its preceding and
following <item> siblings?

On 1/25/10, Martin Honnen <Martin.Honnen@g...> wrote:
> a kusa wrote:
>
> > Source XML:
> >
> > <root>
> > <level1>
> > <st1>
> > <desc><text>sample desc</text></desc>
> > <!-- A <spec> element can occur here as well-->
> > <level2>
> > <item><text>r1 </text></item>
> > <item><text>r2</text></item>
> > </level2>
> > <spec><para>Some spec 1</para></spec>
> > <spec><para>Some spec 2</para></spec>
> > </st1>
> > </level1>
> > </root>
> >
> > Here is my desired output XML:
> >
> > <root>
> >
> > <step1>
> > <text>sample desc</text>
> >
> > <step2>
> > <text>r1</text>
> > </step2>
> >
> > <step2>
> > <text>r2</text>
> > <spec><para>Some spec 1</para></spec>
> > <spec><para>Some spec 2</para></spec>
> >
> > </step2>
> >
> > </step1>
> > </root>
> >
> > Please note that every item under level2 becomes a step2 in my output XML.
> >
>
> The following stylesheet creates the output you describe from the input you
> posted:
>
> <xsl:stylesheet
>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>  version="1.0">
>
>  <xsl:output indent="yes"/>
>  <xsl:strip-space elements="*"/>
>
>  <xsl:template match="@* | node()">
>    <xsl:copy>
>      <xsl:apply-templates select="@* | node()"/>
>    </xsl:copy>
>  </xsl:template>
>
>  <xsl:template match="st1/desc | st1/level2">
>    <xsl:apply-templates/>
>  </xsl:template>
>
>  <xsl:template match="level1">
>    <step1>
>      <xsl:apply-templates select="st1/*"/>
>    </step1>
>  </xsl:template>
>
>  <xsl:template match="level2/item">
>    <step2>
>      <xsl:apply-templates/>
>    </step2>
>  </xsl:template>
>
>  <xsl:template match="level2/item[last()]" priority="3">
>    <step2>
>      <xsl:apply-templates/>
>      <xsl:copy-of select="../following-sibling::spec"/>
>    </step2>
>  </xsl:template>
>
>  <xsl:template match="st1/spec"/>
>
> </xsl:stylesheet>
>
> --
>
>        Martin Honnen
>        http://msmvps.com/blogs/martin_honnen/
>
>
> --~------------------------------------------------------------------
> XSL-List info and archive:
> http://www.mulberrytech.com/xsl/xsl-list
> To unsubscribe, go to:
> http://lists.mulberrytech.com/xsl-list/
> or e-mail:
> <mailto:xsl-list-unsubscribe@l...>
> --~--
>
>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@l...>
--~--

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.