Hi,


This is going to be a xpath query. In the input xml, elemt 'e' is the fifth child of root element. Using the apply template and template match, am at element 'e'. How can i get the value, 5(fifth child), within the template, e. I tried using position, but its value is coming as 1. Also, I do not want to alter anything else, except the line ,
<xsl:value-of select ="position()"/>, which is below <xsl:variable name="posi">.

Dani AI

Generated

— short answer and why.

position() returns 1 because your xsl:apply-templates selects only the <e> node(s), so the context node-list seen by the template contains a single item. To get the element’s index among all children of its parent, count the preceding element-siblings and add one. Replace only the position() call with:

<xsl:value-of select="count(preceding-sibling::*) + 1"/>

This counts element siblings that come before the current node and adds 1 (so your <e> becomes 5). It’s safe when there are whitespace/text nodes between elements because preceding-sibling::* counts elements only.

Notes and alternatives:

  • If you really want to keep using position(), change the apply-templates to select all children (for example /*/*), so the node-list passed to the template contains every child; then position() will reflect the child index. That is a behavioral change, though.
  • If you prefer to keep your existing <xsl:variable name="posi">...</xsl:variable>, simply replace the inner <xsl:value-of select="position()"/> with the count(preceding-sibling::*) + 1 expression shown above.
  • To include text nodes in the count (rare), use count(preceding-sibling::node()) + 1 instead.

Hi,


This is going to be a xpath query. In the input xml, elemt 'e' is the fifth child of root element. Using the apply template and template match, am at element 'e'. How can i get the value, 5(fifth child), within the template, e. I tried using position, but its value is coming as 1. Also, I do not want to alter anything else, except the line ,
<xsl:value-of select ="position()"/>, which is below <xsl:variable name="posi">.

Here also some problem with attachments. Please find the files below

input.xml

<Root>
    <a>123</a>
    <b>321</b>
    <c>897</c>
    <d>011</d>
    <e>456</e>
</Root>

xsl file

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">     
            <xsl:apply-templates select="/*/e"/>      
    </xsl:template>

    <xsl:template match="e">
    <result>
        <xsl:variable name="posi">
            <xsl:value-of select ="position()"/>
        </xsl:variable>
         <xsl:value-of select ="$posi"/>       
        </result>

    </xsl:template>
</xsl:stylesheet>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.