473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XSLT namespace selection (how)?

I am trying to transform an XML document (google base feed) where it's
root element falls within a namespace. Because of this namespace, my
simplistic XSL transformation doesn't (I'm guessing) match/select the
correct elements.

Can someone please explain the matching/selection (xpath?) syntax for
projecting some of the element's data such as <idand
<openSearch:totalResults>?

The example google base feed is:

<?xml version='1.0'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://
a9.com/-/spec/opensearchrss/1.0/' xmlns:gm='http://base.google.com/ns-
metadata/1.0' xmlns:g='http://base.google.com/ns/1.0'
xmlns:batch='http://schemas.google.com/gdata/batch'>
<updated>2007-05-25T18:56:07.358Z</updated>
<openSearch:totalResults>1</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
<id>foo1</id>
</entry><entry>
<id>foo2</id>
</entry>
</feed>

And a simplistic XSL attempting to select the <id>:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:feed='http://www.w3.org/2005/Atom'
version="1.0"
>
<xsl:template match="/">
<xsl:for-each select="feed:entry">
<xsl:value-of select="id" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Much appreciated. Thanks,

Jun 5 '07 #1
5 7287
* Queue wrote in comp.text.xml:
><xsl:template match="/">
<xsl:for-each select="feed:entry">
<xsl:value-of select="id" />
You have two errors here. The first is that feed:entry is not a child of
the root node, they are children of the feed:feed element. So you would
have to say e.g.

xsl:for-each select = 'feed:feed/feed:entry'

This is unlikely the best way, but this would work. Further, the <id>
element is also in the http://www.w3.org/2005/Atom namespace, so you
have to specify the prefix in the value-of aswell, i.e.

xsl:value-of select = 'feed:id'

The rule is quite simple, if what you want to refer to is in a namespace
you have to declare a prefix for the namespace and always specify the
prefix.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jun 5 '07 #2
On Jun 5, 11:37 am, Bjoern Hoehrmann <bjo...@hoehrmann.dewrote:
xsl:for-each select = 'feed:feed/feed:entry'
xsl:value-of select = 'feed:id'
Great, this works. Thank you. Thus for any element declared within a
namespace, in the corresponding XSL all elements within that hierarchy
must be prefixed?

What about the case in the example

<openSearch:totalResults>1</openSearch:totalResults>

Which is contained within the <feedelement (prefixed). I am sure I
can't simply do select="feed:openSearch:totalResults". What about
nested elements within namespaces?

Thank you again,

Jun 5 '07 #3
Queue wrote:
>Thus for any element declared within a
namespace, in the corresponding XSL all elements within that hierarchy
must be prefixed?
No. To match a namespaced name at any given point in an XPath, the path
must use a prefixed name at that point where the prefix is bound to the
correct namespace. So to match
<openSearch:totalResults>1</openSearch:totalResults>
directly, you would use:
select="opensearch:totalResults"

Or, if you needed to specify multiple steps to reach it, you would use
something like:
select="feed:feed/feed:entry/opensearch:totalResults"

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Jun 5 '07 #4
On Jun 5, 1:43 pm, Joseph Kesselman <keshlam-nos...@comcast.net>
wrote:
select="opensearch:totalResults"
select="feed:feed/feed:entry/opensearch:totalResults"
I just tried this from that XML example in the XSL below but I receive
no selection of the data? Shouldn't the whole openSearch prefix be
prefixed itself? Thanks for all the newbie help.

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
xmlns:gm='http://base.google.com/ns-metadata/1.0'
xmlns:g='http://base.google.com/ns/1.0'
xmlns:batch='http://schemas.google.com/gdata/batch'
xmlns:feed='http://www.w3.org/2005/Atom'
version="1.0"
>
<xsl:template match="/">
<div>
<span>total1=<xsl:value-of select="openSearch:totalResults"/>
</span>
<span>total2= <xsl:value-of select="feed:feed/feed:entry/
openSearch:totalResults"/</span>
<xsl:for-each select="feed:feed/feed:entry">
<pre>
<xsl:value-of select="feed:id" />
</pre>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>

Jun 5 '07 #5
* xailor wrote in comp.text.xml:
>On Jun 5, 1:43 pm, Joseph Kesselman <keshlam-nos...@comcast.net>
wrote:
> select="opensearch:totalResults"
select="feed:feed/feed:entry/opensearch:totalResults"

I just tried this from that XML example in the XSL below but I receive
no selection of the data? Shouldn't the whole openSearch prefix be
prefixed itself? Thanks for all the newbie help.

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
xmlns:gm='http://base.google.com/ns-metadata/1.0'
xmlns:g='http://base.google.com/ns/1.0'
xmlns:batch='http://schemas.google.com/gdata/batch'
xmlns:feed='http://www.w3.org/2005/Atom'
version="1.0"
>>
<xsl:template match="/">
<div>
<span>total1=<xsl:value-of select="openSearch:totalResults"/>
Again, here the context node is the document node which usually has a
single child node, the root element of the document, the <feedelement
in your example. 'openSearch:totalResults' matches all openSearch:
totalResults elements that are a child of the context node, but there
is no such child.
<span>total2= <xsl:value-of select="feed:feed/feed:entry/
openSearch:totalResults"/</span>
This would only match the first openSearch:totalResults element, you
likely want this for each entry.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jun 5 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stefan Siegl | last post by:
Hello, I am trying to learn XSLT to use it in another project. I start reading the book "Java and XSLT" and tried the examples and they are went quite fine (how suprising *g*). Then I tried...
3
by: Benjamin Hillsley | last post by:
Hi, I have a xml file that catalogs my cd and dvd collection, and I am currently writing a web page to access this catalog. I would like to include a selection box that lists the CD's by...
1
by: Harry Zoroc | last post by:
I would like to treat an xsd Schema file as XML file and to display the targetNamespace and all the imports. That's it. But the following does not work. Why? I did not enter the stylesheet in the...
4
by: cyclops | last post by:
I'm trying to do XML + XSLT -> Another XML. The source XML contains multiple namespaces and XSLT will handle all possible tags under each name space. ----source---- <document xmlns="..."...
4
by: David S. Alexander | last post by:
How can I do simple subtraction in an XSLT. I want to read a few attribute values from an XML document, calculate their difference, and transform that value to an attribute in the XML output...
1
by: Nick | last post by:
I am working on a website for a client and one of their requirements was to have a mailing list. I decided to XSLT to transform "templates" to HTML so that editing was very easy and less time...
1
by: Rajesh | last post by:
I am trying to pass the namespace, which is in my Original Message, to a Java method for further processing. In the original message the xmlns:xenc namespace is present. I make a call to Java...
6
by: kluge.wolfram | last post by:
Hi, i get stucked on a transformation problem using XSLT. What i need is to copy an XML Tree to an output XML without any automatic changes. Since i used <xsl:copyor <xsl:copy-ofthere occur...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.