0

I have two sections of code that I think should do the same thing but one does not work.

This returns a value

<xsl:variable name="x" select="sources/source[@type='A']"></xsl:variable>
<xsl:value-of select="$x/name"></xsl:value-of>

This does not return a value

<xsl:function name="eul:xx">
    <xsl:param name="root"></xsl:param>
    <xsl:variable name="a" select="$root/sources/source[@type='A']"/>
    <xsl:value-of select="$a" separator=" "></xsl:value-of>
</xsl:function>

<xsl:variable name='x2' select="eul:xx(/)"></xsl:variable>
<xsl:value-of select="$x2/name"></xsl:value-of>

I would like to be able to return something that I can query just like anything I create in the main template. What is funny is that if I select the name element in the function and not after the function is called it also works:

<xsl:value-of select="$a/name" separator=" "></xsl:value-of>

So it looks like something is happening when the value is returned.

2 Answers 2

1

It's a matter of data types. The xsl:value-of instruction generates a text node. Text nodes cannot have child nodes, so the expression $x2/name is meaningless.

Note also that by using the xsl:value-of instruction your function extracts the text values of the referenced nodes - unlike your first version, where $x contains the referenced nodes themselves.

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

3 Comments

That was actually a really good explanation.Is there a way to return somethingwithout usingvalue-of?
@Alex There are plenty of ways to return other data types. Here's just one example: w3.org/TR/xslt20/#element-sequence
The usual way to return a result from xsl:function is by using xsl:sequence. Note also, it's a really good idea to declare the types of both the function result and the parameters, using "as" attributes. It makes it much clearer what's going on, it gives clearer diagnostics when you get it wrong, and it can improve performance.
0

I believe the solution is replace

<xsl:value-of select="$a" separator=" "></xsl:value-of>

with

<xsl:copy-of select="$a"></xsl:copy-of>

2 Comments

It's hard to say what the solution is when the problem is not defined. You never told us why this function is necessary. Note that copying nodes is not the same thing as selecting them: copying creates new nodes, distinct from the original ones - so you might very well notice a difference between your first version and this one when looking at an intersection of node-sets, for example.
Actually, you don't need to return a copy: you can return the original, using <xsl:sequence select="$a"/>

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.