1

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>
De Char
  • 73
  • 1
  • 1
  • 6
  • Your question is far too broad, what are you actually asking? Can the xsl and xsl-fo languages handle trivial xml? - yes; Can common xslt and xsl-fo processors do their job? -> yes. – tolanj Feb 13 '15 at 16:45

1 Answers1

1

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?

Community
  • 1
  • 1
Michael
  • 7,348
  • 10
  • 49
  • 86
  • @DeChar - I'd only recommend using `xsl:for-each` (pull type transformation) if the input data is simple and you're not going to need to extend your XSLT in the future. Otherwise, it's much easier to extend your stylesheet if you use a push type transformation where you use `xsl:apply-templates` and override the built-in templates to handle transformation. – Daniel Haley Feb 13 '15 at 18:13
  • A lot of people find the `for-each` easier to read than templates (which also bring some less obvious / implicit functionality). It's easy to refactor down the line if his requirements change. – Michael Feb 16 '15 at 09:11