开发者

XSLT Advance a node without using for-each

Pretty green on XSLT and one of the systems I'm working on is using it to generate some tables on the front end. Basically performing a query against a db2 instance the result set is being parsed into xml and the output is similar to...

<ResultSet>
    <Row>
        <node1></node1>
        <node2></node2>
        <etc>
    </Row>
    <Row>
    </Row>
</ResultSet>

I'm wondering how to advance a node without being required to use a for-each loop. This is from my understanding of variables inside of XSLT (which is limited).

At the end of the page I have to create a table using the variables that I create above. What is assumed of the result set is that it will return three rows and no more/less. Some of the code from my xslt is as follows...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
    ....after some html population...
    <tbody>
        <tr>
            <td>Column</td>
                <td class="rightAligned">
                    <xsl:call-template name="formatAsCurrencyNoDecimals">
                        <xsl:with-param name="numberToFormat" 
                            select="summarizedLoads/summary/total" />
                    </xsl:call-template>
                </td>
                .....xsl continues for this row....

Once this is done what needs to be done to advance to the next row? My though was that I would have to modify the root template match to <xsl:template match="/summarizedLoads/"> and then call that afte开发者_如何学Gor each row.

Inside of each row I'll need to be creating several variables for use at the end.

Also all rows contain the same amounts of data. Hopefully this is clear as to what I'm trying to do and if anything else is needed from me please let me know.


Suppose you have following XML:

<root>
  <row>1</row>
  <row>2</row>
  <row>3</row>
  <row>4</row>
  <row>5</row>
</root>

To select only 3 rows you can use XPath: //row[position() &lt; 4], e.g. XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="//row[position() &lt; 4]"/>
  </xsl:template>

  <xsl:template match="row">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

Output should be:

<row>1</row>
<row>2</row>
<row>3</row>


The sweet spot in XSLT is when you use nested templates. You've got one template already; let's make another one below the one you currently have where match = "Row". In that template, do all your row-specific stuff. Then call it from within your main template (match = "/") where you want your final rows to be like so:

<xsl:apply-templates select = "./Row[0]"/>
<xsl:apply-templates select = "./Row[1]"/>
<xsl:apply-templates select = "./Row[2]"/>

If you wanted all the rows instead of just the first 3, you would do this instead:

<xsl:apply-templates select = "./Row"/>

The dot stands for the current element. Since we're in the main template, that's the root element ResultSet. /Row means we're applying the first template that matches Row to all the descendant Row elements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜