Wrox Programmer Forums
| 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 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
  #1 (permalink)  
Old September 26th, 2012, 11:30 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 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
  #2 (permalink)  
Old September 26th, 2012, 01:13 PM
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

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
The Following User Says Thank You to mhkay For This Useful Post:
WilliamYou (September 26th, 2012)
  #3 (permalink)  
Old September 26th, 2012, 02: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
  #4 (permalink)  
Old September 26th, 2012, 03:43 PM
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

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
The Following User Says Thank You to mhkay For This Useful Post:
WilliamYou (September 27th, 2012)
  #5 (permalink)  
Old September 27th, 2012, 04: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
  #6 (permalink)  
Old September 28th, 2012, 05: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
  #7 (permalink)  
Old September 29th, 2012, 08: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
  #8 (permalink)  
Old September 29th, 2012, 02:46 PM
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

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
  #9 (permalink)  
Old September 30th, 2012, 07: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
  #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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2017
Location: Los Angeles, California
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you for screenshots


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 01:21 AM
Help with JAXP (NoClassDefFoundError) Satyajit Sahu XSLT 1 July 13th, 2007 03: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 03:31 AM





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