Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 20th, 2007, 03:06 AM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extracting portion of XML ** Need help asap**

I have a requirement where i need to parse/extract a portion of xml based on certain variable which is available in same xml.

for ex:

<booklist>
    <book>
        <category>Fiction</category>
        <titleA>DaysMurder</titleA>
        <price>
            <cents>10</cents>
            <dollar>12</dollar>
        </price>
        <amount>2</amount>
    </book>
    <book1>
        <category>Fiction1</category>
        <titleA>DaysMurder1</titleA>
        <price>
            <cents>101</cents>
            <dollar>121</dollar>
        </price>
        <amount>21</amount>
    </book1>
    <book2>
        <category>Fiction2</category>
        <titleA>DaysMurder2</titleA>
        <price>
            <cents>102</cents>
            <dollar>122</dollar>
        </price>
        <amount>22</amount>
    </book2>
    <value>book2</value>
</booklist>


In the above example having list of books, i need to extract book node based on element value which appears in the end. In this case my output should look like

    <book2>
        <category>Fiction2</category>
        <titleA>DaysMurder2</titleA>
        <price>
            <cents>102</cents>
            <dollar>122</dollar>
        </price>
        <amount>22</amount>
    </book2>

please suggest a way to accomplish this thing, if some one can reply with an xslt that will be really helpful

Reply With Quote
  #2 (permalink)  
Old December 20th, 2007, 05:19 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm unsure exactly what you mean by 'element value which appears in the end'. Can you clarify that please. Do you mean based on the value on the <amount> element, or something else.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old December 20th, 2007, 06:03 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is a poor document design. You shouldn't have book elements named <book1>, <book2>, and so on - it makes the data very hard to process. You would be much better off using attributes: <book number="1"> and so on.

However, in this case the answer isn't difficult, you just want

<xsl:template match="/">
  <xsl:variable name="t" select="booklist/value"
  <xsl:copy-of select="booklist/*[name()=$t]"/>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #4 (permalink)  
Old December 20th, 2007, 09:12 AM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot for your answer. It helped us...Thanks again
Reg
aintvoguish
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting subset of XML using XSLT sentah XSLT 3 September 3rd, 2008 02:44 PM
Want to print some portion of the web page. jigu_2811 C# 1 October 17th, 2006 08:16 AM
Extracting XML from XML ashcarrot XSLT 0 March 15th, 2005 10:52 AM
In Access - look at portion of string usvinyl Access 1 September 23rd, 2004 03:06 PM



All times are GMT -4. The time now is 10:48 AM.


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