 |
| 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
|
|
|
|

August 3rd, 2005, 09:09 AM
|
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Selecting nodes of various namespace
I have come to a brickwall. I need to retrieve a child node of an element that has a partial variable namespace: =>
<xsl:for-each select="a(insert any positive integer here):*/ a(insert same positive integer)/b"/>
Also, would a more effeceint way be:
<xsl:for-each select="a(insert any positive integer here):*//b"/>?
Is that legal syntax? (a//b) I have seen the common case of //b, but i haven't seen a case that specifies a parent then searches for that element from that node.
I have consulted the books and I haven't found anything on these sort of selections. For any given input I do not have any idea of how many different types of tags that i will be recieving. So is there anyway that i can prepare to handle this type of situation?
|
|

August 3rd, 2005, 11:32 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
This looks like a very strange use of namespaces.
But you can do it using
select="*[local-name()=EXPR and namespace-uri()=EXPR]"
where EXPR is any expression, e.g. concat('http://something', $i)
Don't try to select elements based on their namespace prefix, since that's not supposed to convey any meaning.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

August 3rd, 2005, 02:07 PM
|
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
>>Don't try to select elements based on their namespace prefix, since that's not supposed to convey any meaning.
Tell that to Microsoft.
Please pardon my inexperience, but i am still having some trouble with getting my selection to work. I don't know, but it could just be the processor, XSLT 1.0.
I have discovered that more document will only have one type of namespace, st1. So that's good news, however, the local-name can still be completely random. Here is some sample text:
<w:r>
<w:t>
I also want to thank you, Governor Warner, and your fellow governors, for your leadership in hosting this education summit on
</w:t>
</w:r>
<st1:place w:st="on">
<st1:country-region w:st="on">
<w:r>
<w:t>America</w:t>
</w:r>
</st1:country-region>
</st1:place>
<w:r>
<w:t>
âs high schools. It is rare to bring together people with such broad responsibilities and focus their attention on one single issue. But if there is one single issue worth your focused attention â it is the state of
</w:t>
</w:r>
<st1:place w:st="on">
<st1:country-region w:st="on">
<w:r>
<w:t>America</w:t>
</w:r>
</st1:country-region>
</st1:place>
<w:r>
<w:t>âs high schools. </w:t>
</w:r>
I need to collect all of the w:r's. And this should work, however i am only getting the w:r that do not have parents with a namespace of st1:
<xsl:for-each select="st1:*/st1:*/w:r | w:r">
..do stuff..
</xsl:for-each>
What am i doing wrong?
Thank you for all of your help!
|
|

August 3rd, 2005, 04:22 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>>Don't try to select elements based on their namespace prefix, since that's not supposed to convey any meaning.
Tell that to Microsoft.
Could you elucidate? Are you aware of cases where you have to select elements based on the prefix, rather than the namespace URI?
It's hard to tell you why your code isn't working. XSLT and XPath code is context-dependent: to understand what it does you need to know both the static context (in particular, the namespace declarations) and the dynamic context (what's the current node?). Since you've shown your code without any of this context, one suspects that this is where the problem lies.
You say: "For any given input I do not have any idea of how many different types of tags that i will be recieving." Well, you have to have some idea, otherwise you can't even begin to process the data. You can make your stylesheet take default action when it encounters unknown elements - this is what template rules are for, and it suggests you should be using xsl:apply-templates rather than xsl:for-each, though the code snippet you've shown us is so tiny that it's not really possible to use it to come to any conclusions about your design approach.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

August 4th, 2005, 09:54 AM
|
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
My purpose is to grab all of the text elements out of a WordML document. Smart tags can appear in the body of the paragraph, st1:*, so when you scroll over that snippet of text a textbox will appear giving some information. As in the sample text i have given before, if your cursor appears over the word "America" a box would appear telling that America is a place and its region is a country. Smart tags can be used to distinguish people, place, times, etc. They rely on their local names to clarify its text. Thus Word 2003 recognizes all of the tags with the prefix st1 as smart tags and their local name as a genre or characteristic type. The child of the smart tags operates the same way to provide the value. Then the grandchild is the text that uses the smart tag and that is want i want to snatch. I hope that explains things better.
As for my problem: my for-each loop will recognize all of the sibling elements except for those with with a prefix of st1; also I have declared the namespace. Still no luck. What else could be the problem?
|
|

August 4th, 2005, 10:47 AM
|
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Scratch that... I got it:
<xsl:for-each select="*[namespace::st1]/*[namespace::st1]">
why does this not work the same as :
<xsl:for-each select="st:*/st1:*">
|
|

August 4th, 2005, 11:49 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
The first tests if an element has a namespace declaration for prefix st1 in-scope, the second test whether the element name is in the namespace bound to the prefix st1. The first condition is wider than the second.
I still think you should be testing namespace URIs, not prefixes.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

August 5th, 2005, 02:06 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
If all you want are the w:t elements why not just select them, or do you need them within their original paragraphs?
--
Joe ( Microsoft MVP - XML)
|
|

August 5th, 2005, 07:53 AM
|
|
Authorized User
|
|
Join Date: Jun 2005
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
do you mean just having //w:t as my expression? Yea, that would make it a lot easier, but i have to take into consideration that could be tables and shapes with textboxes in the paragraph. i am trying to create the same paragraph verbatim. I hope that is what you were inquiring or implying.
>>The first tests if an element has a namespace declaration for prefix st1 in-scope, the second test whether the element name is in the namespace bound to the prefix st1. The first condition is wider than the second.
I still think you should be testing namespace URIs, not prefixes.
-- I see what you are saying, and that's probably the better route to take.
|
|
 |