0

I want to Get node position as attribute of a element. I means I want to get id value like Aut-01,Aut-02 inside context element. I mentioned below Input, output and tried code. I am using XSLT 2.0

Input :

       <con-group>
            <con c-type="aut">
                <name>
                    <fname>Kent-Dennis</fname>
                </name>
            </con>
            <con c-type="aut">
                <name>
                    <fname>dfr-gvfrt</fname>
                </name>
            </con>
            <con c-type="aut">
                <con-id con-id-type="ABC11"
                    >https://wasq.lk/0000-0002-8551-9535</con-id>
                <name>
                    <fname>Glazier</fname>
                </name>
            </con>
            <con c-type="aut">
                <con-id con-id-type="ABC12"
                    >https://wasq.lk/0000-0002-8551-8535</con-id>
                <name>
                    <fname>Glazier</fname>
                </name>
            </con>
      </con-group>

Output should be :

    <link ref="https://orcid.org/0000-0002-8551-9535">
        <context type="Aut" id="Aut-01">
            <image ref="../../../../command/Templates/Template Art/Auth_.jpg"/>
        </context><s/>
    <link>

    <link ref="https://orcid.org/0000-0002-8551-9535">
        <context type="Aut" id="Aut-02">
            <image ref="../../../../command/Templates/Template Art/Auth_.jpg"/>
        </context><s/>
    <link>

Tried code :

<xsl:template match="con-id">
    <xsl:text>&#x2009;</xsl:text>
    <link ref="{.}">
        <xsl:variable name="aaa" select="count(self::con-id/preceding-sibling::*)+1"/>
        <context type="Aut" id="{$aaa}">
            <image ref="../../../../command/Templates/Template Art/Auth_.jpg"/>
        <context><s/>
    </link>
</xsl:template>

Help me to solve this. I am getting Id value always 1. As my code I don't get output that I want.

user5000
  • 39
  • 6

3 Answers3

2

You are getting 1 each time because con-id has no preceding-siblings. (Elements are siblings if they have the same parent element). You should count preceding-siblings of the parent (but only if the preceding siblings have con-id it looks like)

<xsl:variable name="aaa" select="count(parent::*/preceding-sibling::*[con-id])+1"/>

Alternatively, you could use the preceding axis

<xsl:variable name="aaa" select="count(preceding::con-id)+1"/>

You could also use xsl:number here

<xsl:variable name="aaa">
    <xsl:number count="con[con-id]" />
</xsl:variable>

If you want to use position() you would have to add another template to match con-group and then only select con-id elements (although this would only really work if you didn't have other processing you wanted to do that might clash).

<xsl:template match="con-group">
    <xsl:apply-templates select="*/con-id" />
</xsl:template>

<xsl:template match="con-id">
    <xsl:text>&#x2009;</xsl:text>
    <link ref="{.}">
        <xsl:variable name="aaa" select="position()" />
        <context type="Aut" id="{$aaa}">
            <image ref="../../../../command/Templates/Template Art/Auth_.jpg"/>
        </context><s/>
    </link>
</xsl:template>
Tim C
  • 70,053
  • 14
  • 74
  • 93
0

Check this code:-

<xsl:template match="con">
    <xsl:variable name="aaa" select="position()"/>
<xsl:for-each select="con-id">
    <xsl:text>&#x2009;</xsl:text>
    <link ref="{.}">
        <context type="Aut" id="{$aaa}">
            <image ref="../../../../command/Templates/Template Art/Auth_.jpg"/>
        </context>
    </link>
    </xsl:for-each>
</xsl:template>
Ajeet Singh
  • 1,056
  • 1
  • 6
  • 21
0
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="//con-id">
<link ref="{.}">
        <xsl:variable name="aaa" select="position()"/>
        <context type="Aut" id="{$aaa}">
            <image ref="../../../../command/Templates/Template Art/Auth_.jpg"/>
        </context>
    </link>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
Marc Stroebel
  • 2,295
  • 1
  • 12
  • 21