Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| 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 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
  #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

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


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





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