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 November 5th, 2007, 03:43 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding Additional Namespace and Prefixing Elements

All,

I have the following XML
============
<?xml version = '1.0' encoding = 'UTF-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
            <SignedInfo>
            <KeyInfo>
            </KeyInfo>
         </Signature>
</soap:Envelope>
=========
I want to convert this to the following with xslt
=========
<?xml version = '1.0' encoding = 'UTF-8'?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         <dsig:Signature xmlns="http://www.w3.org/2000/09/xmldsig#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
            <dsig:SignedInfo>
            <dsig:KeyInfo>
            </dsig:KeyInfo>
         </dsig:Signature>
</soap:Envelope>
=========

The Only difference is "Signature" element now it has got namespace "xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" and all elements both "Signature" and the childs underneath have "dsig" namesapce.

Thank you..

 
Old November 5th, 2007, 04:37 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need a modified version of the identity template:

<xsl:template match="Signature">
  <xsl:apply-templates select="." mode="add-ns"/>
</xsl:template>

<xsl:template match="*" mode="add-ns">
  <xsl:element name="dsig:{local-name()}" namespace="http://www.w3.org/2000/09/xmldsig#">
    <xsl:apply-templates mode="add-ns"/>
  </xsl:element>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 5th, 2007, 05:41 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thans for the response, I tried but the output is only getting the values of elemenets... here is the output..

===
<?xml version="1.0" encoding="UTF-16"?>abcd
===
for the xml same as in my original post just added value to "KeyInfo"

 
Old November 5th, 2007, 07:23 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry I didn't spot that Signature was already in the right namespace and you only want to change the prefix. So just change

<xsl:template match="Signature">

to

<xsl:template match="s:Signature" xmlns:s="http://www.w3.org/2000/09/xmldsig#">

Though you can't be 100% sure that the processor will choose the prefix that you want - the theory is that prefixes don't matter.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 5th, 2007, 08:02 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry for not being clear here is what I'm trying to do... There is no namespace reference the original xml looks like

 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">

I want the output like
 <dsig:Signature xmlns="http://www.w3.org/2000/09/xmldsig#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">

adding one more namespace leaving the one already there and adding a prefix to the element...

hope it' clear



 
Old November 5th, 2007, 08:29 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, you're not being clear. You say

There is no namespace reference.

And then you show me XML containing a namespace declaration like:

 the original xml looks like
 <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">

Perhaps I don't know what you mean by "namespace reference" - it's not standard terminology.

At this stage I start to wonder why you want to do what you are doing. Perhaps if you're using non-standard terminology, you don't understand namespaces all that well, and you don't really need to change the prefix at all? So I'd like to take a step back and ask why - why do you want the output that you say you want? Because it's a little bit unusual, so you must have some reason.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 5th, 2007, 09:29 PM
Registered User
 
Join Date: Nov 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, you do have a point, of what I'm doing, I'm trying to overcome bug on a security product. They expect the signature element have two namespace declarations. That product is expecting like below

        <dsig:Signature xmlns="http://www.w3.org/2000/09/xmldsig#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#">
            <dsig:SignedInfo>
            <dsig:KeyInfo>
            </dsig:KeyInfo>
         </dsig:Signature>

thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding Prefix and Namespace to XML Lerxt XSLT 6 November 8th, 2007 03:14 PM
Default Namespace does not display elements. AjayLuthria XSLT 2 April 17th, 2007 10:11 AM
Working with non-Default namespace Elements/Attrib tclotworthy XSLT 3 February 8th, 2007 01:40 PM
How a additional prefix in a namespace comes? diang BOOK: ASP.NET Website Programming Problem-Design-Solution 0 July 13th, 2006 10:40 PM
adding elements in XSLT 1.0 spencer.clark XSLT 3 July 25th, 2005 09:41 AM





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