I am new to xsl. I want to transform an xml from,
<result name="response" numFound="1" start="0">
<doc>
<str name="q">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
</arr>
</doc>
</result>
to,
<result name="response" numFound="2" start="0">
<doc>
<str name="q">what</str>
<str name="suggestion">what1</str>
</doc>
<doc>
<str name="q">what</str>
<str name="suggestion">what2</str>
</doc>
</result>
I could extract the texts, "what1" and "what2" using,
<xsl:template match="/response/result[@name='response']">
<xsl:for-each select="./doc/arr[@name='suggestion']/str">
<xsl:value-of select="normalize-space(.)"/>
<xsl:value-of select="$endl"/>
</xsl:for-each>
</xsl:template>
But I don't know how to enclose the same into output xml format. Can anyone please help...
Additional feature:
Can anyone please tell if I could add a field to the output doc called <float name="score">
which would get incremented by 100 with each doc?
eg) output:
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">1</int>
<lst name="params">
<str name="indent">on</str>
<str name="q">"what"</str>
</lst>
</lst>
<result numFound="2" name="response" start="0">
<doc>
<str name="query">what</str>
<str>what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="query">what</str>
<str>what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="query">what</str>
<str>what3</str>
<float name="score">300</float>
</doc>
</result>
</response>
Can you please tell what function do I suppose to use?
Here is a solution to the OP's comment that updates the question:
This transformation:
when applied on the following (provided in the OP's comment) XML document:
produces the wanted, correct result:
Update:
In an update to his question, the OP requested:
This is extremely easy to achieve. We add just three lines of code to the existing transformation:
When this transformation is applied to this XML document:
the wanted, correct result is produced:
The following stylesheet produces the desired result from your input. It might be too lenient or too strict, but since I don't know your exact requirements or have a schema for the input, it's a best effort. Please use this as a starting point but do proper testing and investigate the meaning of what I provide.
EDIT: In response to your comment: notice how my first template matches on
/*
. This comes down to "any root element". If an XPath expression starts with a single/
, it's always relative to the document root. If it starts with a double//
it basically means anywhere in the current node. If it doesn't start with a slash, it's relative to the current node.In order to process the XML as in your comment, you'd need to change the stylesheet to...
That first template I added is to make sure we override some default template hidden in XSLT. Mind that this will only provide correct output if there's only a single
result
element inresponse
.Like I stated, please learn the use of XSLT and XPath expressions to make sure you understand what's going on in the stylesheet.