Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0
This is the forum to discuss the Wrox book XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition by Michael Kay; ISBN: 9780470192740
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition ISBN: 978-0-470-19274-0 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 Display Modes
  #1 (permalink)  
Old September 26th, 2012, 12:30 PM
Authorized User
Points: 141, Level: 2
Points: 141, Level: 2 Points: 141, Level: 2 Points: 141, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default JAXP XMLFilter confusion.

Hello.

If I have an XML file that contains an element with 10 attributes and I then create an XSLT that selects only 5 of these attributes how many should I see in my XMLFilter implementation?

At present I see all 10 of them when I would have expected have seen only the 5 I selected.

--
William
Reply With Quote
  #2 (permalink)  
Old September 26th, 2012, 02:13 PM
mhkay's Avatar
Wrox Author
Points: 18,277, Level: 58
Points: 18,277, Level: 58 Points: 18,277, Level: 58 Points: 18,277, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,933
Thanks: 0
Thanked 282 Times in 277 Posts
Default

Well, it rather depends whether your XMLFilter is placed before or after the XSLT transformer in the processing pipeline. From the sound of it, your filter appears before the transformer in the pipeline but you are expecting it to appear after. (The JAXP API for setting up pipelines is very confusing, so this is probably an easier mistake to make than you might think.)

However, the usual rules apply: we can't find the bugs in your code if you don't show us your code.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
WilliamYou (September 26th, 2012)
  #3 (permalink)  
Old September 26th, 2012, 03:38 PM
Authorized User
Points: 141, Level: 2
Points: 141, Level: 2 Points: 141, Level: 2 Points: 141, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
Well, it rather depends whether your XMLFilter is placed before or after the XSLT transformer in the processing pipeline. From the sound of it, your filter appears before the transformer in the pipeline but you are expecting it to appear after. (The JAXP API for setting up pipelines is very confusing, so this is probably an easier mistake to make than you might think.)
Good, that will make a change for me.
Quote:
Originally Posted by mhkay View Post
However, the usual rules apply: we can't find the bugs in your code if you don't show us your code.
Thanks for your reply, I've included the code so maybe this will shed some light on the matter at hand.
Code:
try
{
	StreamSource source = new StreamSource(input) ;
	File outputFile = new File(output) ;
	TransformerFactory factory = TransformerFactory.newInstance() ;
	Source styleSource = factory.getAssociatedStylesheet(source, null, format, null) ;
	BinaryTokeniser binaryTokeniser = new BinaryTokeniser() ;
	StreamSource style = binaryTokeniser.getResource("Transformers" + styleSource.getSystemId().substring(styleSource.getSystemId().lastIndexOf('/')))  ;
	binaryTokeniser.Close();
	SAXParserFactory parserFactory = SAXParserFactory.newInstance() ;
	parserFactory.setNamespaceAware(true) ;
	XMLReader reader = parserFactory.newSAXParser().getXMLReader() ;
	SAXTransformerFactory saxFactory = (SAXTransformerFactory) factory ;
	XMLFilterImpl pipeline = null ;
	XMLFilter filter = saxFactory.newXMLFilter(style) ;
	if (getType().equals("XYPlot"))
	{
		pipeline = new XYPipelineFilter(list, section, chart) ;
		((XYPipelineFilter)pipeline).setDateRange(fromDate, toDate) ;
		((XYPipelineFilter)pipeline).setTooltips(getTooltips()) ;
	}
	else
	{
		pipeline = new PNGPipelineFilter(list, section, chart) ;
		((PNGPipelineFilter)pipeline).setDateRange(fromDate, toDate) ;
	}
	pipeline.setParent(reader) ;
	filter.setParent(pipeline) ;
	TransformerHandler serialiser = saxFactory.newTransformerHandler() ;
	serialiser.setResult(new StreamResult(outputFile)) ;
	Transformer t = serialiser.getTransformer() ;
	t.setOutputProperty(OutputKeys.METHOD, "xml") ;
	t.setOutputProperty(OutputKeys.INDENT, "yes") ;
	t.setParameter("selector", list) ;
	pipeline.setContentHandler(serialiser) ;
	pipeline.parse(source.getSystemId()) ;
	outputFile.delete() ;
}
catch (SAXException | TransformerConfigurationException | ParserConfigurationException | IOException e)
{
	e.printStackTrace();
}
--
William
Reply With Quote
  #4 (permalink)  
Old September 26th, 2012, 04:43 PM
mhkay's Avatar
Wrox Author
Points: 18,277, Level: 58
Points: 18,277, Level: 58 Points: 18,277, Level: 58 Points: 18,277, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,933
Thanks: 0
Thanked 282 Times in 277 Posts
Default

It's a while since I did this, but I think you're trying to set up a pipe with four components:

reader -> pipeline -> filter -> serialiser

but then you do this

Code:
        pipeline.setContentHandler(serialiser) ;
	pipeline.parse(source.getSystemId()) ;
which means the pipeline talks directly to the serializer without going via the filter. I think both these two methods should be applied to the filter rather than the pipeline.

But I'm also confused because you're doing this

Code:
Source styleSource = factory.getAssociatedStylesheet(source, null, format, null) ;
and this stylesheet is never used in the pipeline (the filter is using some other stylesheet). Are you applying the XSLT that you think you are?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
WilliamYou (September 27th, 2012)
  #5 (permalink)  
Old September 27th, 2012, 05:04 AM
Authorized User
Points: 141, Level: 2
Points: 141, Level: 2 Points: 141, Level: 2 Points: 141, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
It's a while since I did this, but I think you're trying to set up a pipe with four components:

reader -> pipeline -> filter -> serialiser

but then you do this

Code:
        pipeline.setContentHandler(serialiser) ;
	pipeline.parse(source.getSystemId()) ;
which means the pipeline talks directly to the serializer without going via the filter. I think both these two methods should be applied to the filter rather than the pipeline.

But I'm also confused because you're doing this

Code:
Source styleSource = factory.getAssociatedStylesheet(source, null, format, null) ;
and this stylesheet is never used in the pipeline (the filter is using some other stylesheet). Are you applying the XSLT that you think you are?
Hello again,

and thank you once again for your help. The reader -> pipeline -> filter -> serialiser scenario is correct but if the truth be told I don't really need a serialiser as my XMLFilters take care of generating the output which is why the last thing I do is outputFile.delete(). I am doing it this way as for the moment this is the only way I know how and was based on this book's (E.3.5) example.

I have changed the code as you suggested to :
Code:
        filter.setContentHandler(serialiser) ;
	filter.parse(source.getSystemId()) ;
And I now get an error :
Code:
XTDE0050: No value supplied for required parameter selector
This of course tells me that the stylesheet is indeed now being used, but I set (or thought I set this) parameter at about line 34 in the original code posting! So something still isn't right.

You're confusion regarding the
Code:
Source styleSource = factory.getAssociatedStylesheet(source, null, format, null) ;
is justified but the XML input file contains references to multiple stylesheets so I use styleSource's getSystemId() via my BinaryTokeniser to extract the one I want from the application's resources, hence the StreamSource style a couple of lines later.

--
William
Reply With Quote
  #6 (permalink)  
Old September 28th, 2012, 06:19 AM
Authorized User
Points: 141, Level: 2
Points: 141, Level: 2 Points: 141, Level: 2 Points: 141, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Exclamation

OK, I've reviewed my code and I've now changed it so that :
Code:
XMLFilter filter = saxFactory.newXMLFilter(style) ;
filter.setParent(reader) ;
pipeline.setParent(filter) ;
as I want my filter to be a PostFilter and the last in the pipeline. I then use :
Code:
pipeline.setContentHandler(serialiser) ;
pipeline.parse(source.getSystemId()) ;
to do that actual parsing. However, I still get told that I have not passed the expected parameter(s) to the stylesheet.

Now when I view both my code and the code from the example (E.3.5) I think I can see why. To me the Transformer is never actually used, either in my code or the example's.

If I comment out the code
Code:
Transformer t = serialiser.getTransformer() ;			t.setOutputProperty(OutputKeys.METHOD, "xml") ;
t.setOutputProperty(OutputKeys.INDENT, "yes") ;
t.setParameter("selector", list) ;
I end up with the same error regarding the lack of parameter values, proving to me at least that the Transformer is redundant.

So how do I pass values to the stylesheet prior to parsing?

--
William
Reply With Quote
  #7 (permalink)  
Old September 29th, 2012, 09:00 AM
Authorized User
Points: 141, Level: 2
Points: 141, Level: 2 Points: 141, Level: 2 Points: 141, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default SOLVED JAXP XMLFilter confusion.

In order to get my parameters bound correctly I had to :
Code:
Transformer t = ((net.sf.saxon.Filter) filter).getTransformer() ;
With this in place everything is now working as expected.
--
William
Reply With Quote
  #8 (permalink)  
Old September 29th, 2012, 03:46 PM
mhkay's Avatar
Wrox Author
Points: 18,277, Level: 58
Points: 18,277, Level: 58 Points: 18,277, Level: 58 Points: 18,277, Level: 58
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,933
Thanks: 0
Thanked 282 Times in 277 Posts
Default

Ah yes, I'd forgotten that bug in JAXP that it failed to provide a way of supplying parameters to a transformer created in this way. Unfortunately the JDK has such strong backwards compatibility rules that they deem it impossible ever to add a new method to an existing interface, so such mistakes can never be corrected.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #9 (permalink)  
Old September 30th, 2012, 08:07 AM
Authorized User
Points: 141, Level: 2
Points: 141, Level: 2 Points: 141, Level: 2 Points: 141, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 35
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
Ah yes, I'd forgotten that bug in JAXP that it failed to provide a way of supplying parameters to a transformer created in this way. Unfortunately the JDK has such strong backwards compatibility rules that they deem it impossible ever to add a new method to an existing interface, so such mistakes can never be corrected.
Hello Michael.

Would it be worth the effort to create an addendum to the book?

--
William
Reply With Quote
  #10 (permalink)  
Old November 29th, 2017, 06:26 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Nov 2017
Location: Los Angeles, California
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you for screenshots
Reply With Quote
Reply


Thread Tools
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
JAXP and XSLT 2.0 / XPATH 2.0 ojasrege XSLT 2 November 27th, 2007 10:40 AM
jaxp and xslt2 multiple output aowss XSLT 2 October 10th, 2007 02:21 AM
Help with JAXP (NoClassDefFoundError) Satyajit Sahu XSLT 1 July 13th, 2007 04:53 AM
XML validation with Schema/DTD using JAXP Hendrik XML 0 December 28th, 2004 06:58 AM
JAXP without source document Hendrik XSLT 1 September 28th, 2004 04:31 AM



All times are GMT -4. The time now is 01:22 AM.


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