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 November 14th, 2008, 12:55 PM
Authorized User
 
Join Date: May 2008
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anboss
Default help needed in writing xpath

The input to my xsl is the following xml:

<root>
<message>
<messageId>BPL34</messageId>
<timeStamp>2008-11-14T16:47:24.881</timeStamp>
<description>description for 34 goes here</description>
</message>
<message>
<messageId>BPL92</messageId>
<timeStamp>2008-11-14T16:47:24.883</timeStamp>
<description>description for 92 goes here</description>
</message>
</root>

The input xml contains two message tags. I need to get the description tag value for the messageId BPL34 and my output xml shd be:

<root>
<result>
<description>description for 34 goes here</description>
</result>
</root>

can anyone help me with the xpath expression which i shd use here inorder to get the description tag alone from the input xml?
 
Old November 14th, 2008, 01:02 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

If your document structure is the same. You can use:
/message/messageId[. = 'BPL34']/description

This will select the description node that is a child of messageId, that is a child of message, that stems from the root.

If you want ALL the description nodes, regardless of their location within, use:
//description[.. = 'BPL34']

By the way this is pretty basic stuff that you need if you're going to learn XSLT at all. I would recommend picking up a book, or possibly reading the http://www.w3schools.com/ guides for starters.
 
Old November 14th, 2008, 01:04 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

XPath alone does not suffice to transform the XML, you need XSLT to do that:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:param name="msgId" select="'BPL34'"/>


  <xsl:template match="root">
    <xsl:copy>
      <result>
        <xsl:copy-of select="message[messageId = $msgId]/description"/>
      </result>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
--
  Martin Honnen
  Microsoft MVP - XML
 
Old November 14th, 2008, 01:07 PM
Authorized User
 
Join Date: May 2008
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anboss
Default

Thanks Martin for the response.

My requirement is slightly different. The message tag will be in any order. So, is there a way to get the description tag only when the messageId is BPL34?

 
Old November 14th, 2008, 01:10 PM
Authorized User
 
Join Date: May 2008
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anboss
Default

Thanks for the response..
 
Old November 14th, 2008, 01:21 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Quote:
quote:Originally posted by anboss
 Thanks Martin for the response.

My requirement is slightly different. The message tag will be in any order. So, is there a way to get the description tag only when the messageId is BPL34?

My posted stylesheet, when applied to the XML input you posted, produces the described result.
If your requirement is different now, meaning your XML input and/or your needed result is different, then you need to describe the new XML input and/or the new result you want.

--
  Martin Honnen
  Microsoft MVP - XML
 
Old November 14th, 2008, 01:26 PM
Authorized User
 
Join Date: May 2008
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anboss
Default

I overlooked the xsl that you have posted. Thanks Martin for the response. Requirement accomplished.






Similar Threads
Thread Thread Starter Forum Replies Last Post
XPATH pallone XSLT 4 March 3rd, 2008 10:58 AM
XPATH pallone XSLT 4 November 19th, 2006 07:50 PM
XPath help needed msj_vijay XSLT 1 December 30th, 2005 01:30 PM
Xpath Ma7T XSLT 2 August 16th, 2005 07:54 AM
XPath rupen XML 2 April 19th, 2005 09:43 AM





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