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 July 2nd, 2006, 11:03 PM
Registered User
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Stumped, need XSLT Help

Hello,

Is it possible to get XSLT to transform XML that looks like this:
<foo>
    <bar leftparen="((" rightparen="" num="3" oper="*" />
    <bar leftparen="" rightparen=")" num="2" oper="-" />
    <bar leftparen="" rightparen=")" num="2" />
</foo>

into something that looks like this:
<foo>
   <paren>
     <paren>
       <bar num="3" oper="*"/>
       <bar num="2" oper="-"/>
     </paren>
     <bar num="2"/>
   </paren>
</foo>

"paren" elements are opened and closed based on the number of '(' in each element's leftparen and rightparen attributes.
Can anyone help?
Thanks,

Rob

 
Old July 3rd, 2006, 08:07 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's certainly possible, but it's not easy, and I'm not going to write the code for you...

You might like to tackle an easier problem first: start with

<open-paren level="0"/>
<open-paren level="1"/>
<bar num="3" oper="*"/>
<bar num="2" oper="-"/>
<close-paren level="1">
<bar num="2"/>
<close-paren level="0">

You can tackle that with a recursive approach along the lines:

process-sequence($S):

* take the first element in S
* if it's not an open-paren, copy it, and apply the same rules to the sequence $S[position() != 1]
* if it is an open-paren, output a <paren> element, whose content is generated by calling process-sequence applied to all the elements up to (and excluding) the matching close-paren.

Once you've got that working, think about how to convert the input from the form you gave into the form above - I suspect that part is relatively easy.


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 3rd, 2006, 11:03 AM
Registered User
 
Join Date: Jul 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I knew it would require recursion and have tried that...however I must be doing something wrong as I can't get it to work, the C# transform doesn't appear to like the fact that I haven't closed the tag yet!
Also, I'm not sure what you mean by:

process-sequence($S):

Is that some sort of built in function? I can't find any reference to that anywhere.

 
Old July 3rd, 2006, 11:07 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, process-sequence was just a name I invented for the algorithm so I could refer to it again. It might turn into a named template or XSLT 2.0 function in your actual code.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
I'm stumped MadMutant BOOK: Beginning Access 2007 VBA ISBN: 978-0-470-04684-5 1 July 24th, 2008 05:29 PM
Stumped on XSLT template hayedid XSLT 8 December 19th, 2007 02:14 PM
Completely Stumped Admiral1701 Classic ASP Professional 6 December 5th, 2006 02:35 PM
Stumped: Contains() Method Woes kwilliams XSLT 5 January 19th, 2006 06:44 PM
This program has stumped me, help! :) c0mpgeeksportsfan JSP Basics 1 December 5th, 2004 01:31 AM





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