Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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?

 
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)
 
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?



 
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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/
 
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.

 
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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/
 
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/
 
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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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




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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.