Subject: [xsl] 1) Position of keyed element, 2) Determining unique attribute values From: Timothy Lebo <timothy.lebo@xxxxxxxxxxx> Date: Thu, 12 Jan 2006 06:41:27 -0500 |
In short, I am attempting to transfom something that looks like: <?xml version="1.0" encoding="UTF-8"?> <root> <object id="a"> <attribute name="color" value="red"/> </object> <object id="b"> <attribute name="size" value="big"/> </object> <object id="c"> <attribute name="size" value="small"/> <attribute name="color" value="purple"/> </object> <link fromobject="c" toobject="b"/> </root>
into: <?xml version="1.0" encoding="UTF-8"?> <output> <thing id="1"/> <thing id="2"/> <thing id="3"/> <attributes name="color"> <attribute idref="1" value="red"/> <attribute idref="3" value="purple"/> </attributes> <attributes name="size"> <attribute idref="2" value="big"/> <attribute idref="3" value="small"/> </attributes> <relation fromthing="3" tothing="2"/> </output>
1) Obtaining the positional information of elements in the input tree from different context nodes.
2) Unable to determine all unique values of //attribute/@name to group into the attributes element. (These would then be processed with the solution to #1)
The the 'thing' IDs in the output MUST be non-negative integers, so generate-id is out. I thought that it would be nice (and aesthetic, IMHO) to use the position of the 'object's to assign IDs for the 'thing's. However, when I try to obtain positional information from the input tree with position(), I get the location of the node in the result node set of the xpath that selects the nodes, which is always "1".
Any solutions with XSLT 2.0 constructs would be much appreciated. I haven't followed the developments within the last three years and am having some difficulty appreciating its full benefits.
Highest regards, Tim Lebo
<xsl:template match="/"> <xsl:element name="output"> <xsl:apply-templates select="//object"/> <xsl:apply-templates select="//object" mode="attributes"/> <xsl:apply-templates select="//link"/> </xsl:element> </xsl:template>
<xsl:template match="object"> <xsl:element name="thing"> <xsl:attribute name="id" select="position()"/> </xsl:element> </xsl:template>
<xsl:template match="object" mode="attributes"> <xsl:element name="attributes"> <xsl:attribute name="name" select="'WRONG: attribute elements should be grouped by attribute/@name, not by object/@id'"/> <xsl:apply-templates select="attribute"> <xsl:with-param name="id" select="position()"/> </xsl:apply-templates> </xsl:element> </xsl:template>
<xsl:template match="attribute"> <xsl:param name="id"/> <xsl:element name="attribute"> <xsl:attribute name="idref" select="$id"/> <xsl:attribute name="{@name}" select="@value"/> </xsl:element> </xsl:template>
<xsl:template match="link"> <xsl:variable name="toobjectID" select="@toobject"/> <xsl:element name="relation"> <xsl:attribute name="fromthing" select="key('objects',@fromobj ect)/position()"/> <xsl:attribute name="tothing" select="//object[@id=$toobjectID]/position()"/> <xsl:attribute name="NB" select="'WRONG: ids should be position of input object elmenents (3,2)'"/> </xsl:element> </xsl:template>
and the output is: <?xml version="1.0" encoding="UTF-8"?> <output> <thing id="1"/> <thing id="2"/> <thing id="3"/> <attributes name="WRONG: attribute elements should be grouped by attribute/@name , not by object/@id"> <attribute idref="1" color="red"/> </attributes> <attributes name="WRONG: attribute elements should be grouped by attribute/@name , not by object/@id"> <attribute idref="2" size="big"/> </attributes> <attributes name="WRONG: attribute elements should be grouped by attribute/@name , not by object/@id"> <attribute idref="3" size="small"/> <attribute idref="3" color="purple"/> </attributes> <relation fromthing="1" tothing="1" NB="WRONG: ids should be position of input object elmenents (3,2)"/> </output>
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Supress namespace auto-ns, Michael Kay | Thread | RE: [xsl] 1) Position of keyed elem, Michael Kay |
RE: RE: [xsl] html tables with XSL, Deepak | Date | RE: [xsl] 1) Position of keyed elem, Michael Kay |
Month |