how to do on a counter and default val

2019-08-07 03:47发布

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 &gt;= $start and position() &lt;= $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:)

标签: xslt
1条回答
Explosion°爆炸
2楼-- · 2019-08-07 04:08

Here is an XSLT 2.0 transformation that produces the wanted result:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 exclude-result-prefixes="xs">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pdefStart" as="xs:integer" select="1"/>
 <xsl:param name="pdefRows" as="xs:integer"  select="9999999"/>

 <xsl:variable name="vStart" as="xs:integer" select=
  "(/*/lst/lst/str[@name='start'],
    $pdefStart
    )
     [1]
  "/>

 <xsl:variable name="vRows" as="xs:integer" select=
  "(/*/lst/lst/str[@name='rows'],
    $pdefRows
    )
     [1]
  "/>

 <xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
 </xsl:template>

 <xsl:template match="result">
    <result numFound=
       "{count(doc/arr/str
              [position() ge $vStart
             and
              position() lt $vStart + $vRows
               ]
               )}"
         start="{$vStart}">
        <xsl:apply-templates/>
    </result>
 </xsl:template>

 <xsl:template match=
   "arr[@name='suggestion']/str
          [position() ge $vStart
         and
           position() lt $vStart + $vRows
          ]">
    <doc>
        <xsl:copy-of select="../../str"/>
        <xsl:copy-of select="."/>
        <float name="score">
         <xsl:value-of select="100*position()"/>
        </float>
    </doc>
 </xsl:template>
 <xsl:template match="doc|doc/arr">
    <xsl:apply-templates/>
 </xsl:template>
 <xsl:template match="str"/>

</xsl:stylesheet>

when applied on this XML document (based on the provided, but with more "interesting" values for rows and start):

<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">3</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>

the wanted, correct result is produced:

<response>
   <lst name="responseHeader">
      <int name="status">0</int>
      <int name="QTime">1</int>
      <lst name="params"/>
   </lst>
   <result numFound="3" start="2">
      <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>
      <doc>
         <str name="query">what</str>
         <str>what4</str>
         <float name="score">400</float>
      </doc>
   </result>
</response>

The defaults in the transformation are set in such a way that if no start is specified in the XML document, we assume start = 1 and if no rows is specified, we assume rows = 9999999 -- that is, in the default case we want to get as many str elements as there are in the document.

Here is an example:

<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 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>

This document has no rows and start and when processed with the above transformation, the defaults are used, producing:

<response>
   <lst name="responseHeader">
      <int name="status">0</int>
      <int name="QTime">1</int>
      <lst name="params"/>
   </lst>
   <result numFound="5" start="1">
      <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>
      <doc>
         <str name="query">what</str>
         <str>what4</str>
         <float name="score">400</float>
      </doc>
      <doc>
         <str name="query">what</str>
         <str>what5</str>
         <float name="score">500</float>
      </doc>
   </result>
</response>

Finally, do note: For maximum flexibility the defaults are specified as global parameters and can be passed externally to the transformation.

查看更多
登录 后发表回答