RE: [xsl] 1) Position of keyed element, 2) Determining unique attribute values

Subject: RE: [xsl] 1) Position of keyed element, 2) Determining unique attribute values
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 12 Jan 2006 11:49:37 -0000
position() doesn't give you the position of a node in the input tree, it
gives you its position within whatever sequence of nodes that you are
processing at the time. For the position in the input tree, use xsl:number,
or something like count(preceding::object). This is potentially quite
expensive, if performance is an issue you might be better off to do a
pre-pass copying the tree and allocating numbers as you go, storing the
numbers in a new attribute.

In XSLT 2.0 you can easily get the distinct attribute names using
distinct-values(//attribute/@name).

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Timothy Lebo [mailto:timothy.lebo@xxxxxxxxxxx] 
> Sent: 12 January 2006 11:41
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] 1) Position of keyed element, 2) Determining 
> unique attribute values
> 
> Greetings,
> 
> 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>
> 
> My solution, listed below, has two problems:
> 
> 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
> 
> XSLT Processor: saxonb8.6.1
> 
> 
> My current attempt is:
> 
> <?xml version="1.0"?>
> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform";  
> version="2.0">
> <xsl:output method="xml" indent="yes"/>
> 
> <xsl:key name="objects" match="object" use="@id"/>
> 
> <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>
> 
> </xsl:transform>
> 
> 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