I'd like to create a generic XSLT 2.0 function that would operate on xml like this:
<?xml version="1.0"?>
<foo xmlns:my="http://meohmy.org" xmlns:yours="http://meohmy2.org">
<yours:pet my:type="dog" my:color="red">Cindy</yours:pet>
<yours:pet my:type="cat">Felix</yours:pet>
<yours:pet my:type="cat" my:color="green">Pai Mei</yours:pet>
</foo>
and given a function signature like this:
my:doit('item',/foo/yours:/pet,@my:color)
(Where the first argument is a label, the second is the nodeset I want to report over, and a third for the value I want to output for each node in that second argument's nodeset, whether it has it or not).
I want it return data like this:
info=red;;green
Note that I want an empty placeholder corresponding to the element without the third argument in question, and order matters.
Since I'm doing this a lot in my stylesheet (dozens of times for different reasons), a function seemed a natural way to go. This is what I've come up with so far...
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://meohmy.org"
xmlns:yours="http://meohmy2.org">
<xsl:template match="/">
<xsl:value-of select="my:doit('item',/foo/yours:pet,@my:color)"/>
</xsl:template>
<xsl:function name="my:doit" as="xs:string?">
<xsl:param name="item" as="xs:string"/>
<xsl:param name="value"/>
<xsl:param name="subvalue"/>
<xsl:if test="$value">
<xsl:value-of>
<xsl:value-of select="$item"/>
<xsl:text>=</xsl:text>
<xsl:for-each select="$value">
<xsl:value-of select="current()/$subvalue"/>
<xsl:if test="position() != last()">
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:value-of>
</xsl:if>
</xsl:function>
</xsl:stylesheet>
However when I do this, I get the following value from the function doit():
item=;;
That suggests to me that the <xsl:value-of select="current()/$subvalue"/>
is not correct in the style sheet. I'm close, I can feel it ;> Any suggestions?
Thanks!