473,473 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Purpose of XMLNS in Declaration?

2 New Member
I'm just starting into the process of writing a translator for an XML document. I am confused about xmlns declarations and their affects. According to my Web search, they should not be parsed, yet they are affecting the outcome of my parsing operation. I am using tkxsltproc on a Mac. A minimal example of the XML is as follows:

Expand|Select|Wrap|Line Numbers
  1. <rootLevel xmlns="http://...." xmlns:xsi="http://..." xsi:schemaLocation="http://..." status="Completed">
  2.   <comment>
  3.     <entry>Here it is</entry>
  4.   </comment>
  5. </rootLevel>
  6.  
I want to pull out the comment/entry node. My basic XSL is as follows:

Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/">
  2.     <body>
  3.     <xsl:apply-templates/>
  4.     </body>
  5. </xsl:template>
  6. <xsl:template match="*">
  7.     <xsl:apply-templates/>
  8. </xsl:template>
  9. <xsl:template match="text()">
  10.     <xsl:apply-templates/>
  11. </xsl:template>
  12. <xsl:template match="comment">
  13.     <p><xsl:value-of select="entry"/></p>
  14. </xsl:template>
  15.  
When I apply the XSL to the XML, I get

<body></body>

When I remove JUST the xmlns="http://..." declaration at the rootLevel, I get the result:

<body>
<p>Here it is</p>
</body>

The same happens when I process through the terminal using xsltproc.

What is happening to give the difference between the former and the latter case, and can I put a directive in the XSL stylesheet to always assure the latter translation occurs?

TIA
Sep 1 '07 #1
2 2362
jkmyoung
2,057 Recognized Expert Top Contributor
The xmlns is designed so that people can accept different incoming data from multiple sources, and know what each part is for. For example, a different source might also provide comment nodes, but want them processed in a different way.

In order for your xsl to recognize what to do in each case, the namespaces can be declared in the xslt.

1. Add xmlns:ab="http://..." in the stylesheet node. (ab chosen at random)
2. When referencing the node using any xpath, add the prefix before.
3. eg
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="ab:comment">
  2.     <p><xsl:value-of select="ab:entry"/></p>
  3. </xsl:template>
To have it work with all comment nodes, you can use the local-name() function.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="*[local-name() = 'comment']">
  2.     <p><xsl:value-of select="*[local-name() = 'entry']"/></p>
  3. </xsl:template>
Sep 4 '07 #2
jjw
2 New Member
The xmlns is designed so that people can accept different incoming data from multiple sources, and know what each part is for. For example, a different source might also provide comment nodes, but want them processed in a different way.
Thank you. I understand this now. The references I was reading on this were outdated or incomplete.

In order for your xsl to recognize what to do in each case, the namespaces can be declared in the xslt.

1. Add xmlns:ab="http://..." in the stylesheet node. (ab chosen at random)
2. When referencing the node using any xpath, add the prefix before.
As I understand this, it requires that I create and maintain a namespace document at the given URL referenced by "ab". This is a bit beyond my current level of proficiency and desire (see the comments below the %%%%).

To have it work with all comment nodes, you can use the local-name() function.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="*[local-name() = 'comment']">
  2.     <p><xsl:value-of select="*[local-name() = 'entry']"/></p>
  3. </xsl:template>
This is a bit more at a level I can understand. However, I tried using it to process one node in the XML with only limited success.

%%%%

The scope of my task was to write a general purpose, XSLT engine based XML input routine for an application that graphs scientific/engineering data. The XML data was provided by a third party. I was not expecting to run into the problem I posted ... it almost made me think, my routine was just not working at all.

I now understand, the problem is not with my routine, rather with how the declarations are presented in the XML/XSL files. I also understand, this is not a problem that I can handle as in a "one-size-fits-all" way in my input routine. When a specific namespace declaration exists in an XML, prior to processing through my routine, each user will have to go through the steps to a) remove that namespace declaration in their XML file, b) declare their own namespace to work with their XML+XSL+my routine, or c) use local-name() functions in their XSL. I can leave this choice up to the user -- my part of the project is done.

I am still unsure why my test use of the local-name() function did not work completely, however I suspect it may be due to some sloppy XSL coding on my part ... something I will eventually get past by ongoing trial and error learning.

Thank you for your clarification and help on this - it has saved me from having to write yet another pre-processing filter on my general purpose routine.
Sep 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Matthew Thorley | last post by:
Why does ElementTree.parse convert my xsi to an xmlns? When I do this from elementtree import ElementTree # Sample xml mgac =""" <mgac xmlns="http://www.chpc.utah.edu/~baites/mgacML"...
1
by: Bill Cunningham | last post by:
I'm new to xml but I think It'll be the thing of the future. Hold data and transfer it. Then give it form somehow. That's where I get stuck. <?xml version="1.0" ?> //left out encoding <data>...
3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
4
by: hazz | last post by:
I just haven't been able to get a solid feel for the purpose of what appears to be a declarative statement just above the class below. Anyone with a clear explanation that will finally stick with...
4
by: David S. Alexander | last post by:
I am trying to transform XML to XML using an XSLT in C#, but the root node of my XML is not being matched by the XSLT if it has an xmlns attribute. Am I handling my namespaces incorrectly? My C#...
3
by: dhurwitz | last post by:
I am trying to create an XML spreadsheet for use with Excel. I first saved a simple spreadsheet as an XML file, and now I am trying to recreate that file in code, using .NET 2.0. The beginning of...
3
by: Gary Stephenson | last post by:
I am endeavouring to use .NET v2.0 XML facilities to "roundtrip" reading and writing XML documents - i.e. to end up with _exactly_ what I started with, but I can't seem to figure out how to get an...
3
by: =?Utf-8?B?R3JlZw==?= | last post by:
If I have an XmlDocument with a deep hierarchy of nodes, with a documentElement having an attribute xmlns="http://someurl.com" and all children inheriting that namespace (but not having the xmlns...
2
by: shaun roe | last post by:
I'm trying to follow the recommendations for svg writing at http://jwatt.org/svg/authoring/ and I am generating my svg with xslt. I had some problem with namespaces, and although I think I solved...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.