473,327 Members | 2,016 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,327 software developers and data experts.

HELP - can't add namespace using XSL

I'm trying to validate XML which doesn't include a namespace, so I've
written a schema and I'm attempting to add a namespace to the original
XML using XSLT. The original XML looks like this:

<orderlist>
<order>
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order>
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

and I would like to be transformed to the following:

<orderlist xmlns="urn:MyOrder-schema">
<order>
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order>
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

However, every approach I've taken to writing an XSLT to do this
results in a blank namespace for the next-level elements, such as the
following:

<orderlist xmlns="urn:MyOrder-schema">
<order xmlns="">
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order xmlns="">
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

This empty namespace results in a failed validation. Is there an
approach to adding a namespace to the top-most element of an XML
fragment using XSL such that the namespace of the next-level elements
aren't affected? I really need to be able to validate this XML, so any
help is greatly appreciated.

Thanks in advance

Sep 17 '05 #1
4 1350


n.phelge wrote:
I'm trying to validate XML which doesn't include a namespace, so I've
written a schema and I'm attempting to add a namespace to the original
XML using XSLT. The original XML looks like this:

<orderlist>
<order>
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order>
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>

and I would like to be transformed to the following:

<orderlist xmlns="urn:MyOrder-schema">
<order>
<header>
<ordernum>1313</ordernum>
</header>
</order>
<order>
<header>
<ordernum>1314</ordernum>
</header>
</order>
</orderlist>


Should simply work with

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns="urn:MyOrder-schema">
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

where you then need to decide how you want to transform attributes, if
you want to simply copy them you could add

<xsl:template match="@*">
<xsl:copy-of select="." />
</xsl:template>

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 17 '05 #2
Tempore 15:32:19, die Saturday 17 September 2005 AD, hinc in foro {comp.text.xml} scripsit n.phelge <n.******@sacbeemail.com>:
Is there an
approach to adding a namespace to the top-most element of an XML
fragment using XSL such that the namespace of the next-level elements
aren't affected?


Yes, this is exactly what happens in your example.
The 'xmlns=""' isn't added to the elements; it was already there, by default, hidden.
What you need to do is change the namespace of each and every node in the tree. An identity transform is what you need:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="node()" mode="put_in_new_namespace">
<xsl:with-param name="new_namespace">urn:MyOrder-schema</xsl:with-param>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="*" mode="put_in_new_namespace" priority="1">
<xsl:param name="new_namespace"/>
<xsl:element name="{local-name()}" namespace="{$new_namespace}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()" mode="put_in_new_namespace">
<xsl:with-param name="new_namespace" select="$new_namespace"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>

<xsl:template match="node()" mode="put_in_new_namespace">
<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>
regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Don't send spam. I don't like it and it is illegal.
Sep 17 '05 #3
Tempore 15:42:34, die Saturday 17 September 2005 AD, hinc in foro {comp.text.xml} scripsit Martin Honnen <ma*******@yahoo.de>:
Should simply work with

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns="urn:MyOrder-schema">
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>


Hey, that's clever... never thought that would work;-)

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Don't send spam. I don't like it and it is illegal.
Sep 17 '05 #4
Thanks for the quick response, guys! I knew there had to be a
straightforward way of doing it.

Sep 17 '05 #5

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

Similar topics

6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
0
by: Tressa | last post by:
Sorry to be such a novice about this but..... I have a windows service. It contains two separate files. Both have the same namespace name. I need to pass "bDisplay" into the other file. I am...
0
by: Simon | last post by:
I need call a LoginUser API from MC++ dll, but when I try to call the I have always the same exception: "System.NullReferenceException: Object reference not set to an instance of an object. at...
1
by: a_deano | last post by:
Hi, I am not totally new to programming for Windows but may have bitten off more than I can chew. My problem is, I would like to install a hook (using SetWindowsHookEx) to monitor mouse...
14
by: Tiraman | last post by:
Hi , I would like to use nested namespace . I have 3 namespace as dll's : Namespace A Namespace B Namespace C And i want to have some namespace that contain them all , some thing like
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
3
by: James | last post by:
I'm interested in learning C++. I'm not sure exactly what this means. I'm currently a VB.NET/C#.NET programmer (both Windows Forms and ASP.NET). I am totally clueless when it comes to C++. I've...
2
by: AnalogKid17 | last post by:
Keywords: ASP.NET app with VS2005 running on Win2003 with IIS6, and SQL2000 on a WinXP Box I've betting the following for days... it's driving me insane: Server Error in '/' Application. ...
4
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.