[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Re: Finding the position of an element wrt to other element


Subject: [xsl] Re: Finding the position of an element wrt to other element
From: Rashi Bhardwaj <rashi.bhardwaj@xxxxxxxxx>
Date: Sun, 21 Nov 2010 12:02:59 +0530

Hi there,

with 342, it means the position of that b element which have child c
and relative to first b element (whether it has child c or not); as
specified my xpath

="//b[c][not(@name=preceding::b[child::c]/@name)]/@name

With the above xpath in my sample xml three b element will be select,
namely, b[@name='3'], b[@name='4'] and b[@name='2'], now the position
of these above selected b elements relative to first 'b' element
(b[@name='1']) will be 3 and 4 for the b's which are child of first
<a> and 2 for the b in second <a>.

Hope this give u a clear picture of the desired output.... your kind
responce is awaited....thanks.....
>
> Date: Fri, 19 Nov 2010 15:46:27 +0530
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> From: Rashi Bhardwaj <rashi.bhardwaj@xxxxxxxxx>
> Subject: Finding the position of an element wrt to other element
> Message-ID: <AANLkTinNE=-iCAvs0puyx7GTnOB=yh_QFfJrRFNV9BnR@xxxxxxxxxxxxxx>
>
> Hi,
>
> I want the position of an element (there is a condition for selecting
> that element) relative to other element
>
> Input XML:
> <test>
> <a>
> <b name ='1'></b>
> <b name ='2'></b>
> <b name ='3'>
> <c></c>
> </b>
> <b name ='4'>
>        <c></c>
>        <c></c>
> </b>
> <b name ='4'>
>        <c></c>
>        <c></c>
> </b>
> </a>
> <a>
> <b name ='1'></b>
> <b name ='2'>
> <c></c>
> </b>
> <b name ='3'></b>
> <b name ='4'>
>        <c></c>
> </b>
> </a>
> </test>
>
> XSL I using:
>
> <xsl:template match="/">
>                <html>
>                        <body>
>                        <table>
>                                <tbody>
>                                        <tr>
>                                                <th>
>                                                <xsl:value-of
select="$CustomSelect"/>
>                                                </th>
>                                        </tr>
>                                </tbody>
>                        </table>
>                        </body>
>                </html>
>        </xsl:template>
>        <xsl:variable name="CustomSelect">
>                        <xsl:text>position: </xsl:text>
>                                <xsl:call-template name="test">
>                                </xsl:call-template>
>                </xsl:variable>
>                <xsl:template name="test">
>  <xsl:call-template name="test2">
>  <xsl:with-param name="node" select="//b[c][not(@name
> =preceding::b[child::c]/@name)]/@name"/>
>    </xsl:call-template>
>     </xsl:template>
> <xsl:template name="test2">
> <xsl:param name="node"/>
> <xsl:for-each select="$node">
> <xsl:value-of select="position()"/>
> <xsl:if test="position()!=last()"/>
> </xsl:for-each>
> </xsl:template>
> </xsl:stylesheet>
>
> Output I m getting is position 123, but i want it should count all b
> from top to end at any level and give relative position like here it
> should give 342. I m getting how to go ahead...please help  me out
> thanks......
>
> Rashi Bhardwaj
>
> ------------------------------
>
> Date: Fri, 19 Nov 2010 11:56:44 +0100
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> From: Wolfgang Laun <wolfgang.laun@xxxxxxxxx>
> Subject: Re: [xsl] Finding the position of an element wrt to other element
> Message-ID: <AANLkTi=V_NctD+9Q0pH6wUcsJh=FHnxn0z3XFzUBJcZP@xxxxxxxxxxxxxx>
>
> Sorry, but I don't understand the problem. I fail to see where "342"
> (or 3, 4, 2?) should come from.
>
> When you say "relative position", then there's always two elements
> involved, and you'll have to specify for both how they can be
> identified.
>
> Also, what unit are you thinking of when you mean "relative position"?
> Number of elements between the first and last <a>
> here
>   <a p=3D"1"/><b/><a/><a/><b/><a/>
> could be 4 (including b) or 2 (<a> only).
>
> Are you using XSLT 2.0? If not, I won't be able to help you at all.
> -W
>
> On 19 November 2010 11:16, Rashi Bhardwaj <rashi.bhardwaj@xxxxxxxxx> wrote:
>> Hi,
>>
>> I want the position of an element (there is a condition for selecting
>> that element) relative to other element
>>
>> Input XML:
>> <test>
>> <a>
>> <b name =3D'1'></b>
>> <b name =3D'2'></b>
>> <b name =3D'3'>
>> <c></c>
>> </b>
>> <b name =3D'4'>
>> =A0 =A0 =A0 =A0<c></c>
>> =A0 =A0 =A0 =A0<c></c>
>> </b>
>> <b name =3D'4'>
>> =A0 =A0 =A0 =A0<c></c>
>> =A0 =A0 =A0 =A0<c></c>
>> </b>
>> </a>
>> <a>
>> <b name =3D'1'></b>
>> <b name =3D'2'>
>> <c></c>
>> </b>
>> <b name =3D'3'></b>
>> <b name =3D'4'>
>> =A0 =A0 =A0 =A0<c></c>
>> </b>
>> </a>
>> </test>
>>
>> XSL I using:
>>
>> <xsl:template match=3D"/">
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<html>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<body>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<table>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<tbody>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
> =A0 =A0<tr>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
> =A0 =A0 =A0 =A0 =A0 =A0<th>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
> =A0 =A0 =A0 =A0 =A0 =A0<xsl:value-of select=3D"$CustomSelect"/>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
> =A0 =A0 =A0 =A0 =A0 =A0</th>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =
> =A0 =A0</tr>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</tbody>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</table>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</body>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</html>
>> =A0 =A0 =A0 =A0</xsl:template>
>> =A0 =A0 =A0 =A0<xsl:variable name=3D"CustomSelect">
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<xsl:text>position: </xsl:=
> text>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<xsl:call-=
> template name=3D"test">
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</xsl:call=
> -template>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0</xsl:variable>
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<xsl:template name=3D"test">
>> =A0<xsl:call-template name=3D"test2">
>> =A0<xsl:with-param name=3D"node" select=3D"//b[c][not(@name
>> =3Dpreceding::b[child::c]/@name)]/@name"/>
>> =A0 =A0</xsl:call-template>
>> =A0 =A0 </xsl:template>
>> <xsl:template name=3D"test2">
>> <xsl:param name=3D"node"/>
>> <xsl:for-each select=3D"$node">
>> <xsl:value-of select=3D"position()"/>
>> <xsl:if test=3D"position()!=3Dlast()"/>
>> </xsl:for-each>
>> </xsl:template>
>> </xsl:stylesheet>
>>
>> Output I m getting is position 123, but i want it should count all b
>> from top to end at any level and give relative position like here it
>> should give 342. I m getting how to go ahead...please help =A0me out
>> thanks......
>>
>>
>>
>>
>>
>> Rashi Bhardwaj
>>
>>
>
> ------------------------------
>
> Date: Fri, 19 Nov 2010 07:58:56 -0400
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> From: Piet van Oostrum <piet@xxxxxxxxxxxxxx>
> Subject: Re: [xsl] Finding the position of an element wrt to other element
> Message-ID: <19686.26240.867883.867072@xxxxxxxxxxxxxxxxxxxxxxxxx>
>
> Rashi Bhardwaj wrote:
>>
>  > Output I m getting is position 123, but i want it should count all b
>  > from top to end at any level and give relative position like here it
>  > should give 342. I m getting how to go ahead...please help  me out
>  > thanks......
>  >
>
> If you do:
>
> <xsl:for-each select="$node">
>  <xsl:value-of select="position()"/>
> </xsl:for-each>
>
> You will always get 1 2 3 ... whatever the input is (the nember depending on
the size of the input)
> --
> Piet van Oostrum
> Cochabamba. URL: http://pietvanoostrum.com/
> Nu Fair Trade woonartikelen op http://www.zylja.com
>
> ------------------------------
>
> End of xsl-list Digest
> ***********************************
>



--


Rashi Bhardwaj


Current Thread
Keywords