xsl-list
[Top] [All Lists]

Re: Comparing with a variable position

2002-10-10 00:18:09
Hello Louis,

Louis Meigret wrote:
----- Original Message -----
From: "Joerg Heinicke" <joerg(_dot_)heinicke(_at_)gmx(_dot_)de>

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



<Prev in Thread] Current Thread [Next in Thread>