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 June 27th, 2008, 03:51 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Namespace, node() and local-name()

Hi! I'm new to XML and we are using an application that use XPath 1.0 and I need some help to refer to "vdata 2" from below code

<ns1:message>
<ns1:msgtype>hello</ns1:msgtype>
 <ns1:route>
  <ns1:param>pdata 1</ns1:param>
  <ns1:value>vdata 1</ns1:value>
  <ns1:param>pdata 2</ns1:param>
  <ns1:value>vdata 2</ns1:value>
 </ns1:route>
</ns1:message>

I know how to get there using absolute reference like

/ns1:message/ns1:route/ns1:value[2]/text()

And the solution I'm working on does not use namespace. So I need to use //node() and local-name() construct as below
//node()[local-name()='msgtype']/text().

Please help build the XPath expression using node() and local-name() to get to the second instance of value in the ns1:route block.

Thanks

 
Old June 27th, 2008, 03:58 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you are using XSLT 2.0 then you can do the following:

/*:message/*:route/*:value[2]/text()

If not then you seem to have all the information you need above (and give examples of how to do what you asK0 so I'm not sure what the problem is?

You can put to predicates together like so:

/ns1:message/ns1:route/node()[local-name()='value'][2]/text()

I'm also confused by what you mean by 'doesn't use namespace'. Does this mean you don't know what the namespace will be, or are you worried you don't know what the prefix will be? The prefix is only a shorthand for the namespace, and will match based on the namespace the prefix stands for, not the actual prefix. e.g. if you example above ns1 was the namespace "http://test1.com" then the following would work:

<xsl:value-of xslns:my="http://test1.com" select="//my:message"/>

/- Sam Judson : Wrox Technical Editor -/
 
Old June 27th, 2008, 04:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If you are using an application, like BizTalk, that doesn't have the facility to set namespace to prefix mappings then you can use a combination of local-name() and namespace-uri(). If ns1 is mapped to http://tempuri.com for example:
Code:
/*/*[local-name() = 'route' and namespace-uri() = 'http://tempuri.com']/*[local-name() = 'value' and namespace-uri()= 'http://tempuri.com'][2]/text()

--

Joe (Microsoft MVP - XML)
 
Old June 27th, 2008, 04:19 AM
Authorized User
 
Join Date: Jun 2008
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Sam,

Thanks for the quick reply! Much appreciated.

Sorry for the confusion there. Yes, I meant 'prefix' and not namespace.

The Xpath expression that I'm trying to build here will be stored in a column in a db table and will be read back by an application where the Xpath expression will be checked against the XML to derive "vdata 2"

We are using XPath 1 and XSLT 1, unfortunately.

I tried your solution as below

//node()[local-name()='value'][2]/text()='vdata 2'

And I got what I wanted.

I was really lost in placing the [ ] in the main predicate and the [] for the position.

Many thanks!!! :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Get Node Name without namespace ramesh.kumarm XSLT 1 September 29th, 2006 02:07 AM
Access Namespace Node using XPath pvasudevan XSLT 1 January 2nd, 2006 06:20 AM
Copying Source Node attributes to output node pvsat XSLT 2 November 3rd, 2005 09:46 AM





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