This post is an extension of XSLT transforming an xml.
In this resultant xml got in the above mentioned post, I want to have features like rows and start.
For example) If as a part of input I am giving
<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>
<str name="rows">2</str>
<str name="start">1</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="query">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
<str>what4</str>
<str>what5</str>
</arr>
</doc>
</result>
</response>
by using the mentioned xsl in the previous post mentioned above, I can get the 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>
<!--str name="rows">
2</str>
<str name="start">1</str-->
</lst>
</lst>
<result name="response" numfound="5" start="0">
<doc>
<str name="query">what</str>
<str name="suggestion">what1</str>
<float name="score">100</float>
</doc>
<doc>
<str name="query">what</str>
<str name="suggestion">what2</str>
<float name="score">200</float>
</doc>
<doc>
<str name="query">what</str>
<str name="suggestion">what3</str>
<float name="score">300</float>
</doc>
<doc>
<str name="query">what</str>
<str name="suggestion">what4</str>
<float name="score">400</float>
</doc>
<doc>
<str name="query">what</str>
<str name="suggestion">what5</str>
<float name="score">500</float>
</doc>
</result>
</response>
and when I give start = 2 then I am expecting suggestions to start from what3 after leaving out 0 and 1st suggestion.
Also when I give rows=1, it means I should get only one result starting from "start" position
Also I want to make sure to have default values for rows and start. Hence on browsing net I could find this post: http://remysharp.com/2008/08/15/how-to-default-a-variable-in-xslt/
Is there a better way of specifying a default value for a variable, like using something like default attribute? I tried using default attribute. But seems it doesn't use that.
Also I could see a <xsl:for-each>
iterating over an array. But I don't know how to make it having start values and end values, like if start is 2 and rows is 2 then I want some thing like,
<xsl:for-each select="counter" start="$start" end = "$end">
<doc>
<str name="query">what</str>
<str name="suggestion"><xsl:value-of select="//arr[@name='suggestion'][start]"</str>
</doc>
</xsl:for-each>
Any suggestions please...
My solution:
Tried more and finally happy to learn few things about xsl :) Thanks to Dimitre and G_H(from previous post)..
XSL created:
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="/response/result">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:attribute name="numFound"><xsl:value-of select="count(doc/arr[@name='suggestion']/str)" /></xsl:attribute>
<xsl:apply-templates select="doc/arr[@name='suggestion']" />
</xsl:copy>
</xsl:template>
<xsl:template match="arr[@name='suggestion']">
<xsl:variable name="rows" as="xs:integer">
<xsl:choose>
<xsl:when test="/response/lst/lst/str[@name='rows']"><xsl:value-of select="/response/lst/lst/str[@name='rows']"/></xsl:when>
<xsl:otherwise>10</xsl:otherwise> <!-- default value -->
</xsl:choose>
</xsl:variable>
<xsl:variable name="start" as="xs:integer">
<xsl:choose>
<xsl:when test="/response/lst/lst/str[@name='start']"><xsl:value-of select="/response/lst/lst/str[@name='rows']"/></xsl:when>
<xsl:otherwise>0</xsl:otherwise> <!-- default value -->
</xsl:choose>
</xsl:variable>
<xsl:for-each select="str">
<xsl:if test="position()-1 >= $start and position() <= $rows+$start">
<doc>
<xsl:copy-of select="parent::node()/preceding-sibling:: str[@name='query']" />
<xsl:variable name="nameAttributeValue" select="parent::node()/@name" />
<xsl:element name="str"><xsl:attribute name="name"><xsl:value-of select="$nameAttributeValue" /></xsl:attribute><xsl:value-of select="." /></xsl:element>
<float name="score">
<xsl:value-of select="100*position()"/>
</float>
</doc>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Input:
<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>
<str name="rows">2</str>
<str name="start">2</str>
</lst>
</lst>
<result name="response" numFound="1" start="0">
<doc>
<str name="query">what</str>
<arr name="suggestion">
<str>what1</str>
<str>what2</str>
<str>what3</str>
<str>what4</str>
<str>what5</str>
</arr>
</doc>
</result>
</response>
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>
<str name="rows">2</str>
<str name="start">2</str>
</lst>
</lst>
<result name="response" numfound="5" start="0">
<doc>
<str name="query">what</str>
<str name="suggestion">what3</str>
<float name="score">300</float>
</doc>
<doc>
<str name="query">what</str>
<str name="suggestion">what4</str>
<float name="score">400</float>
</doc>
</result>
</response>
Finally I might be needed to change the numfound parameter based on the given number of docs. Again thanks a lot for your guidance:)
Here is an XSLT 2.0 transformation that produces the wanted result:
when applied on this XML document (based on the provided, but with more "interesting" values for
rows
andstart
):the wanted, correct result is produced:
The defaults in the transformation are set in such a way that if no
start
is specified in the XML document, we assumestart = 1
and if norows
is specified, we assumerows = 9999999
-- that is, in the default case we want to get as manystr
elements as there are in the document.Here is an example:
This document has no
rows
andstart
and when processed with the above transformation, the defaults are used, producing:Finally, do note: For maximum flexibility the defaults are specified as global parameters and can be passed externally to the transformation.