|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

November 16th, 2006, 05:32 AM
|
|
Authorized User
|
|
Join Date: May 2006
Location: Haarlem, , Netherlands.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|

November 16th, 2006, 05:42 AM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
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
|

November 16th, 2006, 08:06 AM
|
|
Authorized User
|
|
Join Date: May 2006
Location: Haarlem, , Netherlands.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

November 16th, 2006, 08:23 AM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
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
|

November 16th, 2006, 09:23 AM
|
|
Authorized User
|
|
Join Date: May 2006
Location: Haarlem, , Netherlands.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

November 16th, 2006, 09:41 AM
|
 |
Wrox Author
Points: 12,735, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,923
Thanks: 0
Thanked 82 Times in 80 Posts
|
|
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
|

November 16th, 2006, 10:11 AM
|
|
Authorized User
|
|
Join Date: May 2006
Location: Haarlem, , Netherlands.
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |