I have xml like this

<mainProcedure>
    <proceduralStep>
      <proceduralStep>
        <proceduralStep>
          <proceduralStep>
          </proceduralStep>
        </proceduralStep>
      </proceduralStep>
      <proceduralStep>
        <proceduralStep>
        </proceduralStep>
      </proceduralStep>
    </proceduralStep>
 </mainProcedure>

the child node name for mainProcedure node is all same and no attributes and text.

i need to find out which proceduralStep node is clicked and its position.

Dani AI

Generated

Quick summary for : with identical, attribute-less nested proceduralStep elements the only stable identifier is tree position. Predicates (the [...] syntax mentioned) are correct for selecting the Nth sibling, but to compute the index of a clicked node use these XPath approaches depending on the scope you want.

  • Index among siblings (same parent): count(preceding-sibling::proceduralStep) + 1
  • Absolute ordinal among all proceduralStep in the document (document order): count(preceding::proceduralStep) + 1
  • When you are already iterating a node-set in XSLT (eg. xsl:for-each or apply-templates over siblings) use position().

To build a path with indexes like /mainProcedure/proceduralStep[2]/proceduralStep[1]/... in XSLT 1.0, iterate ancestor-or-self::proceduralStep and for each output concat('/proceduralStep[', count(preceding-sibling::proceduralStep)+1, ']'). Example core pattern:

<xsl:for-each select="ancestor-or-self::proceduralStep">
  <xsl:value-of select="concat('/proceduralStep[', count(preceding-sibling::proceduralStep)+1, ']')"/>
</xsl:for-each>

If working in the browser with an XML DOM and an element reference from a click, compute the sibling index with JavaScript:

function siblingIndex(el){
  var i = 0;
  while(el = el.previousElementSibling){
    if(el.localName === 'proceduralStep') i++;
  }
  return i + 1;
}

Notes and cautions: whitespace/text nodes and namespaces change what counts as a sibling—use previousElementSibling or test nodeType===1 and match localName/namespaceURI if needed. If you need stable references across edits, add an id attribute rather than relying on positional addresses.

use [] bracket as if

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates select="a"/>
        </root>
    </xsl:template>
    <xsl:template match="a">
        <xsl:apply-templates select="b"/>
    </xsl:template>
    <xsl:template match="b">
        <xsl:apply-templates select="c"/>
    </xsl:template>
    <xsl:template match="c">
        <xsl:apply-templates select="d1[@att2][ position()=1]" mode="a">
            <xsl:with-param name="kn" select="d1[@att2]"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="d1[not(@att2)]"/>
    </xsl:template>
    <xsl:template match="d1">
        <a>
            <b>
                <c>
                    <d1>
                        <xsl:value-of select="."/>
                    </d1>
                </c>
            </b>
        </a>
    </xsl:template>
    <xsl:template match="d1" mode="a">
        <xsl:param name="kn"/>
        <a>
            <b>
                <c>
                    <xsl:for-each select="$kn">
                        <d1>
                            <xsl:value-of select="."/>
                        </d1>
                    </xsl:for-each>
                </c>
            </b>
        </a>
    </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.