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

Re: Comparing with a variable position

Subject: Re: Comparing with a variable position
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Thu, 10 Oct 2002 09:18:09 +0200
comparing with
Hello Louis,

Louis Meigret wrote:
----- Original Message -----
From: "Joerg Heinicke" <joerg.heinicke@xxxxxx>

Thank you Jörg, this seems elegant, two questions though.


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


1) Why the above template rather than the simpler
<xsl:template match="container">, I'm especially puzzled by the <xsl:copy> (I'm a newbie).

this is a template, that copies everything node by node (or attribute by attribute) to the output. It's called identity transformation and can also be found in the spec: http://www.w3.org/TR/xslt#copying. I have used it, because you don't want only to copy <container/>, but also everything between the separators to the output.


<xsl:template match="container/text()">
  <sub-container>
    <xsl:value-of select="."/>
  </sub-container>
</xsl:template>

<xsl:template match="separator"/>

If "thus several node are possible" instead of text "mixed content"


2) Well I do not know if it is "instead", I meant "or". Is mixed content contardictory with several nodes (text and elements)?

That's ok. A text node is also a node of course. node() includes text(), comment(), processing-instruction() and elements.


So for your problem we need the grouping. It's called Muenchian Method and you will find it quite often in the archives or at http://www.jenitennison.com/xslt/grouping/muenchian.xml.

<xsl:key name="nodes-by-sep" match="container/node()[not(self::separator)]" use="generate-id(following-sibling::separator)"/>

<xsl:template match="container">
<xsl:copy>
<xsl:apply-templates select="node()[generate-id() = generate-id(key('nodes-by-sep', generate-id(following-sibling::separator)))]"/>
</xsl:copy>
</xsl:template>


<xsl:template match="container/node()">
<sub-container>
<xsl:copy-of select="key('nodes-by-sep', generate-id(following-sibling::separator))"/>
</sub-container>
</xsl:template>


Here identity transformation is not used anymore, but it's possible. If you replace the <xsl:copy-of/> in the last template with <xsl:apply-templates/> (select attribute is the same), the nodes are copied node by node and not the whole subtree at once. Maybe you need it, if you want to further process the subtree.

And a question to your XML source. Isn't it possible to change this, so that you don't need this grouping? I don't know where it comes from, but if it's an output of anything, can this anything not output <sub-container/>s too instead of separators?

Regards,

Joerg


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



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.