Xml – Convert string to node-set

jstlxmlxslt

In my xsl file,
I am receiving a separate xml document (beside the main xml document
that I’m transforming) as a string parameter (param)

say my param name is seconddoc

<xsl:param name="seconddoc"></xsl:param>

and the param value is following (again, i'm getting the whole thing as a string)

<products>
    <product>
        <id>1</id>
        <name>pro-1</name>
    </product>
    <product>
        <id>2</id>
        <name>pro-2</name>
    </product>
    <product>
        <id>3</id>
        <name>pro-3</name>
    </product>
</products>

I can print the entire string as following

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

But I want iterate the data (string) instead of getting entire value at once.
my end goal is to load this data to a select option.

I tired like this:

<select>
    <xsl:for-each select="$seconddoc/products/product">
        <option value="{id}">
            <xsl:value-of select="name" /></option>
    </xsl:for-each>
</select>

but I'm getting TransformerException. "Invalid conversion from 'java.lang.String' to 'node-set'.

update:

This is what I have in my jsp page

<x:transform xml="${mainxmldoc}" xslt="${xslt}">
<x:param name="seconddoc" value="<%=xmlString %>"/>
</x:transform>

Best Answer

You're probably using the built-in XSLT 1.0 processor that comes with the JDK, which is a version of Xalan. There's no standard way in either XSLT 1.0 or 2.0 to invoke an XML parser (which is what you need to do to convert a string to a node). You either need to do the conversion outside the transformation (passing a node as the parameter), or use an extension that does the job. I seem to remember that Xalan's implementation of exslt:node-set() might do this; check it out. Alternatively, since you're in the Java world, a whole lot of things would be much easier if you upgraded to XSLT 2.0 (meaning Saxon). I'm not sure how well Saxon plays with JSP though (it can certainly be done, but I don't know if you can use the x:transform tag library). Saxon has a saxon:parse extension function, which in the draft 3.0 specifications is replaced by a standard function parse-xml().

Related Topic