Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 9th, 2004, 12:24 PM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default conditional xslt processing based on java array

I'm relatively new to XSLT, and I'm trying to come up with an elegant solution to the following problem -

I have a JSP that keeps a String array containing field names. These field names correspond to node names in an XML document. Each user of my web app can select a subset of these field names, and it is this subset of nodes and their values that I want to display using XSLT. I don't see a way to use this array in the stylesheet to conditionally display nodes without using a custom extension function - am I missing an easy solution?

Reply With Quote
  #2 (permalink)  
Old September 10th, 2004, 02:27 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

So do you have something like a JSP page which contructs a select element with the arry values and then the users can select one or more options and you then invoke a transform of some data?
If so then you can pass the relevant String valuess to your transformer using its proprietary method before transforming and access to the values will be available via gloal xsl:param element(s) in the stylesheet. For more specific advice on the method you need to show some of the Java transform code and perhaps a small sample of xml data and xslt.

If not please explain further.

--

Joe (Co-author Beginning XML, 3rd edition)
Reply With Quote
  #3 (permalink)  
Old September 14th, 2004, 06:57 AM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well basically I have a set of values in a Java data structure that I need to iterate over using XSLT. For example, I have a Java String array that contains the following data:

[node1, node3, node5]

Then I have XML that may look like this -

<page>
  <node1>Some text data</node1>
  <node2>Some more text data</node2>
  <node3>Even more text here...</node3>
  <node4>Still more text</node4>
  <node5>The final line of text.</node5>
</page>

I would like to be able to write an elegant stylesheet that would display only the textual data for the node names contained in the string array - in this case node1, node3, and node5. Any ideas?



Reply With Quote
  #4 (permalink)  
Old September 14th, 2004, 07:06 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you're using Saxon 8, you can pass an array of strings directly as a parameter to your stylesheet (it will appear as a sequence of strings). Then the code is simply:

<xsl:param name="in" as="xs:string*" required="yes"/>

<xsl:template match="page">
  <page>
    <xsl:copy-of select="*[name() = $in]"/>
  </page>
</xsl:template>

You can do much the same in XSLT 1.0, but you will have to wrap the array of strings into an XML document structure, and pass that document as a parameter.



Michael Kay
http://saxon.sf.net/
Reply With Quote
  #5 (permalink)  
Old September 14th, 2004, 08:29 AM
Registered User
 
Join Date: Sep 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the quick reply Michael. I'm using the latest stable version of Xerces as my parser. Not sure how to best go about wrapping the string array in a document structure - can you elaborate? Thanks in advance.

Reply With Quote
  #6 (permalink)  
Old September 14th, 2004, 08:58 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Use DOM interfaces: createDocument, createElement, etc. Tedious but it works. Alternatively create the document as text:

for (int i...)
 out.println("<a>" + string[i] + "</a>")

and then parse the text; but take care about escaping special characters.

I wouldn't have suggested the XSLT 2.0 solution if I didn't think it was easier.



Michael Kay
http://saxon.sf.net/
Reply With Quote
  #7 (permalink)  
Old June 28th, 2006, 06:58 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,

seems you are one of the few Saxon specialists ;)
Michael, I need to return an array from Java method, e.g. String[].

Can you help me further on how to process the array on XSL side (I'm using Stylus Studio with Saxon XSLT processor).

What exactly do I need to code in XSLT to process this array?
Sorry, I didn't understand exactly what you were explaining...



Quote:
quote:Originally posted by mhkay
 If you're using Saxon 8, you can pass an array of strings directly as a parameter to your stylesheet (it will appear as a sequence of strings). Then the code is simply:

<xsl:param name="in" as="xs:string*" required="yes"/>

<xsl:template match="page">
<page>
    <xsl:copy-of select="*[name() = $in]"/>
</page>
</xsl:template>

You can do much the same in XSLT 1.0, but you will have to wrap the array of strings into an XML document structure, and pass that document as a parameter.



Michael Kay
http://saxon.sf.net/
Reply With Quote
  #8 (permalink)  
Old June 28th, 2006, 07:32 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The best place for Saxon-specific questions is the saxon-help list or forum at sourceforge.net.

I think the original thread was about calling XSLT from Java, and I think you're doing the opposite - calling Java from XSLT. You can return an array from an extension function. The way you process it depends on what's in the items of the array, but basically you process it as a sequence, for example

for $i in my:javaMethodReturningArray()
return ....

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array like processing (Xpath 1 / XSLT 1) akentanaka XSLT 1 July 2nd, 2008 02:22 AM
Conditional processing in XSL francislang XSLT 2 March 18th, 2008 12:04 AM
XSLT conditional sum calculation lxken XSLT 2 August 3rd, 2007 10:37 PM
Key based array in VBScript? rgalehouse Classic ASP Basics 3 March 1st, 2004 06:05 PM
conditional statement based on attribute value dancbishop XSLT 2 November 7th, 2003 12:50 PM



All times are GMT -4. The time now is 10:58 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.