INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Processing specific nodes...‏

Processing specific nodes...‏

(OP)
I'm using Apache FOP, embedded in a Java desktop application, to create PDFs from a data XML file and an XSLT file.

Say the data XML file is a list of items of the form

<items>
<item>apple</item>
<item>apples</item>
<item>orange</item>
<item>water melon</item>
...
</items>

I want to be able to tell the XSLT to only process specific nodes (say "apple" and "orange"). Note that each item will have an unique name - no repeats, but some names could contain the names of other nodes (such as apples contains apple).

In the Java code which kicks off the PDF render, I create a java.util.ArrayList and pass that to the transformer using setParameter( "theList", theList ). In the XSLT, when I use <xsl:value-of select="$theList" />, I'll get in the output [apple, orange].

However, what I would need is to take the array and iterate over it, calling a template for each matching item. Is this possible?

Also, given that "apples" contains "apple", using to something like the contains/matches functions would not work. The example data XML is simplified - in reality the items would not necessarily be in English and could contain punctuation, such as "My Item - 1.5, and more"...meaning that using the ',' to (somehow) tokenise would not work either.

Any ideas?


Thanks in advance,

Bernard.

RE: Processing specific nodes...‏

Welcome to Tek-Tips, Bernard.

First, I need to ask a few questions.

Is your solution constrained to use a serialized ArrayList? I am not a Java programmer, so I am not sure exactly how ArrayList serializes data that contain commas, but this definitely makes the XSLT side a bit more difficult.

If you can serialize the ArrayList to an XML document, then probably the most straightforward approach is to use that document created from the ArrayList as the input document to the XSLT, and use the document() function to pull information from the data document. This would be my preferred solution because the XSLT is much simpler to create and understand.

If the serialized ArrayList must be processed as a string, then the complexity goes up a bit. If, as I imagine, the serialized ArrayList might contain quoted strings, the complexity of the XSLT to parse the string is not impossible, but not for the beginner either (eee my post regarding scanning JSON using XSLT 1.0 at http://www.tek-tips.com/viewthread.cfm?qid=1709035).

So, Bernard, how much flexibility do you have in your solution?

Tom Morrison
Hill Country Software

RE: Processing specific nodes...‏

(OP)
Hi Tom,

No, I'm not constrained to use an ArrayList; I could have just as easily chosen Vector. My thinking is that I need to specify one or more items to process/render (at runtime) and I assumed it would be possible to pass in an array/list of those string items and then in the XSLT loop over that list OR as the entire data XML is processed, the array/list is checked against for matching items.

I've read elsewhere it should be possible to pass an XML nodelist (via Java using setParameter) and then somehow the XSLT can reference the nodelist...but I can't seem to find a concrete example...most posts simply say use a nodeset.

Also, FOP uses XALAN which (I believe) is XSLT 1.0 and so I'm limited to the solutions. Again from what I've read, an XSLT 2.0 processor would give me more options.

In the end I don't really care what solution I use...assuming it doesn't significantly hit memory/time. I have complete control over the development so to answer your question I have complete flexibility! Simply put, I need to render a specific set of nodes in a data XML file and tell the XSLT file which nodes they are!


Thanks,

Bernard.

RE: Processing specific nodes...‏

Hi Bernard,

I will create an example based upon the assumption that you can place your 'selection list' in an XML document, and show you how to use document() to pull stuff from your actual data document. Expect something tomorrow.

My current work involves replacing a huge collection of hard-coded PCL reports with XML pipeline, using Apache FOP. One of the biggest issues is discerning the actual data elements among all the noise surrounding the creation of a PCL document. Once I assemble the data elements it is rather easy to serialize and produce the report. So - this is all to say I am spending a lot of time in creation of XSL that feeds Apache FOP.

(By the way, you can 'preprocess' your XML using any XXSL process you want, to create XSL-FO outside the scope of Apache FOP. At least on the command line, there is a way to introduce the XSL-FO.)

Tom Morrison
Hill Country Software

RE: Processing specific nodes...‏

(OP)
I do have another idea which works using tokens.

In Java, I determine the items which I want rendered/processed. I concatenate those item names into one string, delimited by a token, say '|'. If the '|' is used in any item name, I'll have to use a '||' and so on until I find a safe token. I also add '|' to the front and back, ending up with say '|apple|orange|'. I pass this to the transformer using setParameter.

In the stylesheet, when I can then use something like apply-templates or for-each and in the select only match item names (with a '|' added either side) with the large concatenated string (thanks Frank for the suggestion!).

I'll give this a go and see what happens...

RE: Processing specific nodes...‏

That will work - I have used the technique myself. I would be somewhat concerned about how well it scales if you are processing large documents in a heavy production environment, but worry about that after you get it working.

Tom Morrison
Hill Country Software

RE: Processing specific nodes...‏

(OP)
I got sidetracked from looking at a string concat/token solution but managed to figure out how to pass a Java object into the XSLT and access it. Here it is hoping it'll help someone in the future...

In Java, create your Java object and set it as a parameter into the javax.xml.transform.Transformer:

ArrayList<String> list = new ArrayList<String>();
list.add( "apple" );
list.add( "orange" );
transformer.setParameter( "list-of-items-to-print", list );

In the XSLT...

The header needs to know about the Java object: <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo arrayList" xmlns:arrayList="http://xml.apache.org/xalan/java/java.util.ArrayLi...">

and you need a parameter definition for the incoming ArrayList: <xsl:param name="list-of-items-to-print" value="" />

In the guts of XSLT, I had test code to ensure the data was coming in...

<fo:block>
list-of-items-to-print: <xsl:value-of select="$list-of-strings" />
list-of-items-to-print.size(): <xsl:value-of select="arrayList:size( $list-of-items-to-print )" />
list-of-items-to-print[ 0 ]: <xsl:value-of select="arrayList:get( $list-of-items-to-print, 0 )" />
list-of-items-to-print[ 1 ]: <xsl:value-of select="arrayList:get( $list-of-items-to-print, 1 )" />
</fo:block>

To actually use this list of items, instead of iterating over it, I used the contains() method to check if the current item (given by XLST iterating over items) is present in the list...

<xsl:template match="items">
<xsl:apply-templates select="./item" />
</xsl:template>

<xsl:template match="item">
<xsl:if test="arrayList:contains( $list-of-items-to-print, string( item ) )">
<fo:block>Found '<xsl:value-of select="item"/>'</fo:block>
</xsl:if>
</xsl:template>

Note that all the above Java/XSLT code is transcribed from a larger chunk of real code and so it may not compile...but should be close to the real thing.


Cheers,

Bernard.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close