De Char
De Char

Reputation: 73

xsl or xsl-fo to handle dynamic xml

I have an xml file which can be dynamic (meaning the number of rows can be 0, 1, 2 or many in the following xml example). How does the xsl (or xsl-fo) handle this case? Any examples or pointers would be greatly appreciated.

<form>
    <table>
        <row>
            <date>2012-02-10</date>
            <departure>Boston</departure>
            <arrival>NYC</arrival>
            <typeOfTransport>Flight</typeOfTransport>
            <estimatedCost>$300.00</estimatedCost>
        </row>
        <row>
            <date>2012-02-12</date>
            <departure>NYC</departure>
            <arrival>Boston</arrival>
            <typeOfTransport>Flight</typeOfTransport>
            <estimatedCost>$200.00</estimatedCost>
        </row>
    </table>
</form>

Upvotes: 1

Views: 89

Answers (1)

Michael
Michael

Reputation: 7448

That'll use an XSL for-each (for reuse with multiple matching XML elements etc.):

<xsl:for-each select="form/table/row">
  <!-- Content -->
</xsl:for-each>

It uses an XPath expression to specify which node set to process - in this case row under form and table.

The function assignment's content will execute / repeat for each (hence the name) matched node.

If there are none (0 rows) it won't get called. For two rows it'll get called twice.

Many people will refer to it as a for-each "loop" - but that's a misnomer (there's no way to break out of an XSL for-each because it's not a loop).

See the W3Schools tutorial:

http://www.w3schools.com/xsl/el_for-each.asp

Also see this question, it'll help you understand the scope / context of what you're doing:

What's the difference between XSLT and XSL-FO?

Upvotes: 1

Related Questions