1

I want to get the value of a specific node, specified by its id attribute. But the behaviour of my XSL parser, Saxon, is not how I expected it to work.

This is my XSL code:

<xsl:template match="synonyme">
    <xsl:element name="corr">
        <xsl:value-of select="@connecteur" />
        <xsl:value-of select="/liste/connecteur[@id=@connecteur]/forme[1]" />
    </xsl:element>
</xsl:template>

I just matched a tag named synonyme which has a connecteur attribute. My code outputs the value of this attribute.

I also want to output the value of another node which has an id attribute matching the connecteur attribute of my currently matched synonyme tag. But no results are ever found for this query, the second value-of always has empty output.

If I write, e.g. [@id='c160'], where c160 is the exact same thing that is output by the first value-of, it works! But not when comparing to the @attribute of my matched node. How can I fix this?

The XML is basically

<liste><connecteur id="c160"><forme>foo</forme></connecteur>
       <connecteur id="c161"><synonyme connecteur="c160" /></connecteur>
</liste>

and the expected output in place of the synonyme is <corr>c160 foo</corr>.

1 Answer 1

2

The predicate you use:

[@id=@connecteur]

is looking for an element with two attributes - id and connecteur- with equal values. To look for an element with an id attribute whose value matches the value of the current element's connecteur value, you need to use:

[@id=current()/@connecteur]

See: https://www.w3.org/TR/xslt/#function-current


A better solution would be to define a key as:

<xsl:key name="ref" match="connecteur" use="@id" />

then use:

<xsl:value-of select="key('ref', @connecteur)/forme" />

to resolve the cross-reference.

See: https://www.w3.org/TR/xslt/#key

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.