[xsl] Cleaning text away from context position ...

Subject: [xsl] Cleaning text away from context position ...
From: Soren Kuula <dongfang@xxxxxxxxxxx>
Date: Thu, 28 Jul 2005 11:39:18 +0200
Hi,

I'm toying with a stylesheet that copies an XML document .. and adds an attribute named id:

For the root element, id is 1
For some other element, id is that of the parent, plus ',' plus the child cardinality of the element (so first child of the root is 1,1, second child of the root is 1,2, second child of third child of the root is 1,3,2 etc).


Something like:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="*|@*|comment()|processing-instruction()">
<xsl:param name="id" select="''"/>
<xsl:param name="sep" select="''"/>
<xsl:variable name="qid" select="concat($id, $sep, position())"/>
<xsl:copy>
<xsl:attribute name="id">
<xsl:value-of select="$qid"/>
</xsl:attribute>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()">
<xsl:with-param name="id" select="$qid"/>
<xsl:with-param name="sep" select="','"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>


Applying it to itself, I get
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; id="1" version="1.0">
<xsl:template id="1,5" match="*|@*|comment()|processing-instruction()">
<xsl:param id="1,5,3" name="id" select="''"/>
<xsl:param id="1,5,5" name="sep" select="''"/>
<xsl:variable id="1,5,7" name="qid" select="concat($id, $sep, position())"/>
<xsl:copy id="1,5,9">
<xsl:attribute id="1,5,9,2" name="id">
<xsl:value-of id="1,5,9,2,3" select="$qid"/>
</xsl:attribute>
<xsl:apply-templates id="1,5,9,4" select="*|@*|comment()|processing-instruction()|text()">
<xsl:with-param id="1,5,9,4,3" name="id" select="$qid"/>
<xsl:with-param id="1,5,9,4,5" name="sep" select="','"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


- not quite what I wanted, as text nodes are included in the context list and affect context positions.

What to do about it? I want to preserve text nodes. Seems I can't select and recurse on all element nodes and all text nodes separately (getting a nice context list of elements only), as that would mess up the order of things.

Any good ideas? Need to recurse in the sibling direction instead?

Soren

Current Thread