|
|
 |
| 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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |
|

January 14th, 2009, 12:00 PM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
change namespace
Hi,
I have the following xml
<Data xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"xmlns:icns="urn:NewBusiness_XXX"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<createdDate>11/12/2009 at 09:21:38</createdDate>
<product_code>ABC</product_code>
</Data>
I would like to change the namespace icns to urn:NewBusiness_ABC
i.e i would like to get the following.
<Data xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"xmlns:icns="urn:NewBusiness_ABC"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<createdDate>11/12/2009 at 09:21:38</createdDate>
<product_code>ABC</product_code>
</Data>
I have tried various methods but nothing works any hints ?
|

January 14th, 2009, 12:02 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 626
Thanks: 0
Thanked 88 Times in 88 Posts
|
|
Do you use XSLT 2.0 or 1.0?
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|

January 14th, 2009, 12:07 PM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
i'm using
XSLT 1.0
|

January 14th, 2009, 12:25 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 626
Thanks: 0
Thanked 88 Times in 88 Posts
|
|
Here is an XSLT 1.0 stylesheet that does the trick here for me (tested with Saxon 6.5.5):
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:icns="urn:NewBusiness_ABC"
version="1.0">
<xsl:template match="@* | text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="document('')/xsl:stylesheet/namespace::*[local-name() = 'icns']"/>
<xsl:copy-of select="namespace::*[not(local-name() = 'icns')]"/>
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|

January 14th, 2009, 12:31 PM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi Martin,
Sorry i didn't explain clearly..
the new namespace should be a dynamic one
urn:NewBusiness_ABC
where ABC is the prodoct code from the xml
|

January 14th, 2009, 01:01 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Location: Germany
Posts: 626
Thanks: 0
Thanked 88 Times in 88 Posts
|
|
I am not sure that is possible. With XSLT 2.0 there is an instruction http://www.w3.org/TR/xslt20/#creating-namespace-nodes to create a namespace node but I don't know how to achieve the same with XSLT 1.0.
Maybe someone else has an idea.
__________________
Martin Honnen
Microsoft MVP - XML
My blog
|

January 15th, 2009, 05:31 AM
|
 |
Wrox Author
Points: 12,642, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 Posts
|
|
Just change this:
<xsl:element name="{name()}" namespace="{namespace-uri()}">
to compute the namespace URI that's needed, e.g.
<xsl:element name="{name()}"
namespace="urn:NewBusiness_{../productCode}">
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|

January 15th, 2009, 06:00 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks Micheal,
The solution works if i have it in a seperate style sheet. But not i the following case
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:icns="urn:NewBusiness_ATV"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:template match="/">
<soapenv:Envelope>
<xsl:call-template name="buildSoapHeader"/>
</soapenv:Envelope>
</xsl:template>
<xsl:template name="buildSoapHeader">
<soapenv:Header>
<icns:IplusHeader>
<icns:freeFormatText>Sample Text</icns:freeFormatText>
</icns:IplusHeader>
</soapenv:Header>
</xsl:template>
<xsl:template match="@* | text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="urn:TEST">
<xsl:copy-of select="document('')/xsl:stylesheet/namespace::*[local-name() = icns']"/>
<xsl:copy-of select="namespace::*[not(local-name() = 'icns')]"/>
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
|

January 15th, 2009, 06:18 AM
|
 |
Wrox Author
Points: 12,642, Level: 48 |
|
|
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 3,900
Thanks: 0
Thanked 80 Times in 78 Posts
|
|
You can copy a namespace node to add an extra namespace declaration to an element, but that will never change the name of the element. The name of the element (its local name and namespace URI) are determined solely by the name and namespace attributes of the xsl:element instruction. If you need to compute the namespace URI, then the computation must be done in the namespace attribute, e.g.
namespace="{document('')/xsl:stylesheet/namespace::*[local-name() = icns']}"
(though that seems a pretty odd way to compute it, since you know at the time you write your stylesheet what namespaces it declares)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|

January 15th, 2009, 06:30 AM
|
|
Authorized User
|
|
Join Date: Jan 2009
Posts: 23
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Thanks for the reply.
But after the transformation i'm loosing the prefix icns
i get the following..
<Print_Data xmlns="urn:Test"
i want to get the resulting xml with the prefix icns please explain how i can acheive this
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |