1

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>
6
  • Which XSLT processor are you using, and does it have any way to pass in the parameter as a node set in the first place? Or failing that can it pass you a URI to the additional XML that you could then load using the document function? Jan 30, 2013 at 22:01
  • Thanks Ian Roberts. I'm new to xslt. I don't think I'm using any additional processor. the xml file i'm getting from a jsp page. and its being generated dynamically. So no URI. How can I pass it as nodeset from a JSP any suggestion? Jan 30, 2013 at 22:18
  • Could you edit the question to include the relevant part of the JSP page where you are calling the XSLT transform? Jan 30, 2013 at 22:23
  • This is what I have in my JSP page........ <x:transform xml="${mainxmldoc}" xslt="${xslt}"> <x:param name="seconddoc" value="<%=xmlString %>"/> </x:transform> Jan 30, 2013 at 22:25
  • Seems similar to this question stackoverflow.com/questions/3959497/… Try using exslt:node-set but I doubt it will work. Jan 31, 2013 at 1:37

3 Answers 3

3

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().

2
  • I did try exslt:node-set() as suggested in other topic in stackoverflow but it didn't work. Any idea how do i pass a node as parameter? Jan 31, 2013 at 16:32
  • Sorry, I'm not familiar with JSP and <x:transform>. Jan 31, 2013 at 16:56
3

it looks like a param cannot be convert to node-set. but a variable can! so the way i solved it.

First, declare a variable and put the parameter value into the variable. Then convert the variable to node-set. Now we can iterate the node-set

here is the full code

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

    <xsl:template match="/">
        <xsl:for-each select="exslt:node-set($stringVar)/node()">
            <select>
                <xsl:for-each select="/products/product">
                    <option value="{id}">
                        <xsl:value-of select="name" />
                    </option>

                </xsl:for-each>
            </select>
        </xsl:for-each>
    </xsl:template>
2
  • actually, it didn't work! i had the similar data in the main xml file and apparently it was pulling the data from there!! Jan 31, 2013 at 23:46
  • Thanks for this. Important detail in this solution. From what I was seeing (not entirely positive), you need to use <xsl:variable><xsl:value-of/></xsl:variable> and not <xsl:variable select="" />
    – rainabba
    Sep 8, 2015 at 20:19
2

With the <x:parse> tag, you can process your content this way:

<x:parse var="doc">
    <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>
</x:parse>

And then target the desired node with XPath:

<x:out select="$doc/products/product/..." />
1
  • i have to access it from xsl file, not jsp. if I contain the string with x:parse tag i get compile error on xsl Jan 31, 2013 at 16:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.