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 November 16th, 2006, 05:32 AM
Authorized User
 
Join Date: May 2006
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default Testing tree structure

I would like to know if there is a quick way with xslt to check if an element in a xml document is at a valid position in the element tree structure defined in the xsd schema?
 
Old November 16th, 2006, 05:42 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Not really.

With a schema-aware stylesheet in XSLT 2.0 you can validate a document (or any subtree) against a schema, but the effect is a fatal error if the document is invalid, which doesn't seem to be what you are looking for.

Perhaps you could explain the background to the requirement.

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

I want to test the hierarchy of all elements in the xml tree in a non schema aware processor.
I can check it with an external document (doctree.xml)with all the paths in it(generated from the xsd) so I could test the path as stringvalue for every element

I build the path string with the expression
concat('//',string-join(for $i in (ancestor-or-self::node()[name()]) return $i/local-name(),'/'))

I now can search the elementtree in the xml like:
select="if doc('doctree.xml')//path[.= concat('//',string-join(for $i in (ancestor-or-self::node()[name()]) return $i/local-name(),'/'))])"

but this seems to be a bit too complicated to me
 
Old November 16th, 2006, 08:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to explain the requirement more clearly.

You have a set of possible paths in some file doctree.xml, yes? And you now want to test whether one of these paths matches some other document, say instance.xml?

Your proposed expression if doc('doctree.xml')//path[.= concat('//',string-join(for $i in (ancestor-or-self::node()[name()]) return $i/local-name(),'/'))]) works entirely within a single document, so it's not clear what it's trying to achieve.

Do you want to know whether all the paths in doctree have a match, or whether a specific one does, or whether a particular node in instance.xml matches one of the paths, or what???

Instead of generating a data file containing paths, why not generate an XSLT stylesheet or XQuery that gives you the answer directly?

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

Yes I want to test if a path of a particular node in instance.xml matches the value of a <path> element in paths.xml

like this:
instance.xml
<elementA><elementB>Value of B</elementB></elementA>

paths.xml
<paths>
    <path>//elementA</path>
    <path>//elementA/elementB</path>
</paths>
when I want to test element B I'll test the string value "//elementA/ElementB"
when I find this value in the paths.xml//paths/path its OK
 
Old November 16th, 2006, 09:41 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'd suggest that instead of generating paths.xml as data, you generate

<xsl:template match="//elementA" mode="test"/>
<xsl:template match="//elementA/elementB" mode="test"/>
<xsl:template match="*" mode="test">NO MATCH</xsl:template>

and import this into your stylesheet. You can then test element B as

<xsl:variable name="x">
  <xsl:apply-templates mode="test"/>
</xsl:variable>
<xsl:if test="$x = 'NO MATCH'">...

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

Great, thanx this is what helps me a lot. I already had tried it with the * path description but I didn't use it in combination with mode and variable. Now only hoping I don't have recursive elements





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with tree data structure vidhya_venkat C++ Programming 0 June 14th, 2006 01:13 PM
tree data structure pandjie Java Basics 1 January 16th, 2006 05:15 AM
Creating a Tree Structure pazzuzu C++ Programming 2 February 26th, 2005 12:07 PM





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