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

November 9th, 2006, 09:50 PM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Nested tags with different values
Hi
I have the following initial XML file:
<ST>
<A>
<B>
<B1 atr="val1"/>
</B>
<C>
<C1>valC1</C1>
<C2>valC2</C2>
</C>
</A>
<A>
<B>
<B1 atr="val2"/>
</B>
<C>
<C1>valC11</C1>
<C2>valC22</C2>
</C>
</A>
</ST>
Now at the beginning, I was not interested at what I had between the C tags, so I used {(A/B/B1/@atr)[1]} and {(A/B/B1/@atr)[2]} to differentiate between the two B/B1 tags. Here is part of my xsl file:
<res>
<tag1> tag_atr="{(A/B/B1/@atr)[1]}"
tag_atr="{(A/B/B1/@atr)[2]}">
</tag1>
</res>
Now I want to include also what is between the C tags for each of the two different values of B/B1, so that I would get in the end document:
<res>
<tag1> tag_atr1="val1"
tag_atr2="valC1">
</tag1>
<tag1> tag_atr1="val2"
tag_atr2="valC11">
</tag1>
</res>
So as you can see, depending on:
1) if I have a C tag (which might not be the case for any of the A tags, in which case the tag_atr2 should not be there)
+
2) the values between the C1 and C2 are equal or not, or other such combinations (of course of the C tag is present), I have to display something similar with what I wrote above for my end document, but with different values for tag_atr2.
I currently try to achieve all this using <xsl:if> statements, but I can't seem to have the corresponding values for tag_atr2. I always get the same value (I'm presuming valC1), event though valC1 and valcC11 are different. It is hard because the tags are the same (for A, B, B1, and C), only the values of the 'atr' attribute and of the 'C1' and 'C2' tags change.
I hope I was clear on the problem, and sorry for the long post. I use XSLT 2.0 and Saxon 8. Thank you for any input regarding this!
Michael
|
|

November 10th, 2006, 10:48 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
It strikes me that you are going about this the wrong way. The "XSLT way" to do this kind of thing is to process the tree with a recursive descent using template rules. Something like:
<xsl:template match="A">
<tag1>
<xsl:apply-templates/>
</tag1>
</xsl:template>
<xsl:template match="B"/>
<xsl:template match="B1">
<xsl:text>tag_atr="</xsl:text>
<xsl:value-of select="@atr"/>
</xsl:text>
</xsl:template>
<xsl:template match="C">
... etc ...
You'll find this makes it a lot easier to change your code the next time your requirements change.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

November 10th, 2006, 03:36 PM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
|
|
The problem is that I need all the values from B1 and C1,2 under the same tag tag1. And also, I need to apply the values to attributes, and not to tags, which makes it even harder to use the template-match combination. My mistake for writing the first post wrong; the output should actually be:
<res>
<tag1 tag_atr1="val1"
tag_atr2="valC1">
</tag1>
<tag1 tag_atr1="val2"
tag_atr2="valC11">
</tag1>
</res>
You are right in processing the tree recursively, but when I have to leave open the end ">" for a tag (like the "tag1" tag) so that I can inlcude different attributes in their when I parse the xml file, I don't know how to achieve all this.
Thank you for your constant help!
Michael
|
|

November 10th, 2006, 04:26 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>You are right in processing the tree recursively, but when I have to leave open the end ">" for a tag (like the "tag1" tag) so that I can inlcude different attributes in their when I parse the xml file, I don't know how to achieve all this.
You mustn't think of the output of XSLT in terms of tags. The output is a tree of nodes. I was puzzled by your output format, which had things that looked like XML attributes outside a start tag. If you concentrate on creating element and attribute nodes, and thinking of the output as a tree, you won't have such problems.
I'd recommend you do some reading around XSLT. It seems to me that you'd benefit from understanding the underlying concepts a bit better.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

November 10th, 2006, 04:30 PM
|
|
Authorized User
|
|
Join Date: Nov 2006
Posts: 28
Thanks: 0
Thanked 1 Time in 1 Post
|
|
You are right; my view of the output must be wrong. But as usual, time is not on my side, but I will try and do some more reading to grasp the concept more clearly.
Thank you again for all your help.
Mihai
|
|
 |