Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 August 23rd, 2006, 04:11 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default Testing XML if it is wellformed

I've been searching the book XSLT2 ans Path2 for a way how I can test external xml documents if it is well formed or not.

My question is two ways
What is the easiest way to test if an xml document is well formed and/or well balanced before I want to process the document with my xslt. Does the saxon processor provides a way to test well formed xml for instance in a .bat file before calling an xslt template.
 and
Is it possible to test nodes in an external document with the document() or doc()function without having an input xml document. Or do I always have to use a dummy xml as input if I only want to test a node in an external document with the document function.
 
Old August 23rd, 2006, 04:17 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well technically all XML is well formed, otherwise it's not XML.
Most, if not all, parsers have the ability to load a document. At this stage they can tell you if it's an XML document, and usually if not why not. In MSXML for example:
Code:
var bLoaded = XmlDoc.load(<path to doc>;
if (bLoaded)
{
  //document is XML
}
else
{
  //document is not XML, check XmlDoc.parseError for further details.
}
Each parser does it slightly differently so you'll have to state which one you intend to use if you need further help and can't find the documentation.

--

Joe (Microsoft MVP - XML)
 
Old August 23rd, 2006, 04:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I think the adjective "well-formed" is rather unfortunate, because talking about "testing XML to see if it is well-formed" suggests that there can be XML that isn't well-formed. In fact all XML is well-formed, if it's not well-formed then it isn't XML.

The way to test whether an input file contains well-formed XML is to try parsing it with an XML parser, and if you get an error then it isn't.

XSLT and XPath can only process well-formed XML. If the input isn't well-formed you'll get an error from the XML parser long before the XSLT or XPath processor kicks in.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 24th, 2006, 07:13 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx for the info. I was trying to do it with the saxon processor inside an xslt document. with the document() function.
I want to use the saxon 2.0 processor and have to test xml against a specific standard and generate a findings report of it. I don't know what the content of the .xml document is, I only know what node should be in it and want to test if it is a wellformed document and is including the node I want to test, before I even start the report.xsl.

Does this mean I first have to parse/load the xml document (even if it is a big one) in Java before I can call the xslt template? or can i call some java command inside the xslt?
 
Old August 24th, 2006, 10:49 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

With Saxon (or any JAXP processor) you can define a URIResolver, which is a simple Java class that is called to process any URI passed to the document() function. In your URIResolver you can call the XML parser, and trap any error that it reports. If the document is OK you can return it, if it's not OK you can return a dummy document containing the error information. You don't have to parse the document twice, because you can return the parsed document tree as the response from the URIResolver if parsing is successful.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old August 25th, 2006, 12:53 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx that is a great solutions, I will try that.

The extra options for saxon8: -it and -im, are also of some use:
I can call the saxon8.jar with an option -it and than I don't need a source for my stylesheet and use the document function in my stylesheet. I can also use the -im option to start my transformation in a specific mode.

thanx again





Similar Threads
Thread Thread Starter Forum Replies Last Post
Xml parser testing Manoah XML 1 July 11th, 2008 04:10 AM
testing integer rjonk XSLT 2 July 27th, 2006 03:11 PM
asking for help in beta-testing Luckasoft Pro PHP 1 October 18th, 2004 10:24 PM
Testing for integer MattLeek Excel VBA 8 March 7th, 2004 09:15 AM
testing mark C# 0 June 4th, 2003 08:37 PM





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