XPath 2.0 dynamic generation of CSV file

Here should go questions about transforming XML with XSLT and FOP.
William
Posts: 42
Joined: Sun Jul 15, 2012 12:26 pm
Location: London

XPath 2.0 dynamic generation of CSV file

Post by William »

Hello all,

I have an XML file that is structured similar to this:

Code: Select all


<foo>
<bar>
<items>
<item/>
<item/>
<.../>
</items>
<sets>
<set/>
<set/>
<.../>
</sets>
<items>
<item/>
<item/>
<.../>
</items>
<sets>
<set/>
<set/>
<.../>
</sets>
</bar>
</foo>
I also have an XSLT that I use to extract the /foo/bar/items/item elements as CSV. However, what I would really like to do is make the transformer dynamic so that I can set some params to tell it that I want to extract ./items/* or ./sets/* as CSV.

Is this doable? I would appreciate your thoughts on this please.

This is being built and tested using the IDE but will be end up in a Java application.

--
William
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XPath 2.0 dynamic generation of CSV file

Post by adrian »

Hi,

It is doable, but it depends on how your XSLT is written.
Note that you can't use variables or parameters in the patterns from xsl:template/@match, but you can use them in xsl:for-each.
Here's a simple example:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:param name="groupName" select="'sets'"/>
<xsl:param name="itemName" select="'set'"/>
<xsl:template match="/">
<xsl:for-each select="//*[name() = $groupName]">
<!-- Per group -->
<xsl:for-each select="*[name() = $itemName]">
<!-- Per item -->
<xsl:message select="text()"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
William
Posts: 42
Joined: Sun Jul 15, 2012 12:26 pm
Location: London

Re: XPath 2.0 dynamic generation of CSV file

Post by William »

Hi Adrian,

Thank you for that looks interesting. I'll give it a go and see what comes out in the wash.

--
William
Post Reply