[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] how to handle structured data from Java


Subject: Re: [xsl] how to handle structured data from Java
From: Robert Koberg <rob@xxxxxxxxxx>
Date: Mon, 10 Jan 2005 19:16:11 -0800

Kevin Rodgers wrote:
Robert Koberg writes:

Kevin Rodgers wrote:

I need to pass an attribute value to a Java method implemented by a
co-worker and save the resulting object in a variable (since it's a
relatively expensive operation), then access the object's string
components.  Should I write an extension function in Java to call his
method and convert the object into a DOM Node that would be stored via
<xsl:value-of name="variable" select="extension:function(@attribute)">?
How would I then access its subnodes -- via select="$variable/component-x"?

You really don't want to use extensions.



Or is there a better way?

I think so. Have your co-worker create a class that implements javax.xml.transform.URIResolver. It should check for the value coming in as an href. When the resolver sees your attribute value, have him return an appropriate XML Source. Then you can do the following in your XSL:


<xsl:variable name="myvar" select="document(@myattr)"/>


OK, but where do I reference HisClass?

URIResolver only declares the resolve method, which returns a Source.
But the Source interface only declares 2 methods, getSystemId and
setSystemId, neither of which is defined to return a dynamic value like
an XML string.

And since the document function takes a URI, shouldn't the select
expression be something like
	"document(concat('http://server/HisServlet?foo=', @myattr))"
If so, then why does he need to implement URIResolver as HisClass --
couldn't he just implement HisServlet?


There are several ways to handle the situation. It might be appropriate to use an XMLFilter chain and never use the document function. This removes the concern of gathering source XML in the transformation. As for a URIResolver, it allows the user to gather source XML as defined by the URIResolver.

For example, in the resolve method, you could:

<xsl:apply-templates select="document('foo')"/>
....
Source source = null;

File file = project.resolveFile(href);

if (file.exists()) {
  source = new StreamSource(new FileInputStream(file));
} else if (href.equals("someObject")) {
  source = project.getObjectAsSaxSource(href);
} else {
  source = new StreamSource(new StringReader("<file-not-found/>"));
}

return source;

best,
-Rob



Thanks,


Current Thread
Keywords
xml