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 February 29th, 2012, 05:30 PM
Authorized User
 
Join Date: Feb 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default Replacing the namespace

Good day all,
I have the following xml name space that I want to replace:
<CommonRecord xmlns="http://www.ed.gov/FSA/COD/2010/v3.0d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ed.gov/FSA/COD/2010/v3.0d CommonRecord3.0d.xsd">

Below is my xslt transforming code. However, I do not see this as being replaced.
<xsl:stylesheet version="2.0"
xmlns:v="http://vocado.vsm-systems.com/xsd/vocado-tx"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:default="http://www.ed.gov/FSA/COD/2011/v3.0e" exclude-result-prefixes="default">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<!--Define the latest usde name space-->

<xsl:variable name="latestUsdeNameSpace" select='"http://www.ed.gov/FSA/COD/2011/v3.0e"'/>



<!-- This template will plug the latest name space in -->
<xsl:template match="//default:*">
<xsl:element name="{local-name()}" namespace="{$latestUsdeNameSpace}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>

<!-- Template to copy all the nodes from the existing xml -->
<xsl:template match="@* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>



</xsl:stylesheet>
 
Old February 29th, 2012, 05:52 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You have the default namespace declared as the new namespace, but are wanting to match the old one.

xmlns:default="http://www.ed.gov/FSA/COD/2011/v3.0e"

should be

xmlns:default="http://www.ed.gov/FSA/COD/2010/v3.0d"

Also, there is no need for the "//" in front of your match statement.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
madowl (March 6th, 2012)
 
Old February 29th, 2012, 05:57 PM
Authorized User
 
Join Date: Feb 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thank you for that Sam. However, the default could be anything. Regardless of what it is, I want all xml files running through this xslt to inherit http://www.ed.gov/FSA/COD/2011/v3.0e. How do I achieve this?
 
Old February 29th, 2012, 06:32 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Then just replace the pattern in the template that matches default:* with match="*", and get rid of the existing match="*" template.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 29th, 2012, 06:55 PM
Authorized User
 
Join Date: Feb 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Thank you Mr.Kay. I am a bit new at this so please bear with me. That did only a partial replace though. I am left with
<CommonRecord xmlns="http://www.ed.gov/FSA/COD/2011/v3.0e">http://www.ed.gov/FSA/COD/2010/v3.0d CommonRecord3.0d.xsd

My changes are as folows:
<xsl:stylesheet version="2.0"
xmlns:v="http://vocado.vsm-systems.com/xsd/vocado-tx"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:default="http://www.ed.gov/FSA/COD/2011/v3.0d">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<!--Define the latest usde name space-->

<xsl:variable name="latestUsdeNameSpace" select='"http://www.ed.gov/FSA/COD/2011/v3.0e"'/>



<!-- This template will plug the latest name space in -->
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="{$latestUsdeNameSpace}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>

<!-- Template to copy all the nodes from the existing xml -->
<!--<xsl:template match="@* | node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>-->



</xsl:stylesheet>
 
Old March 1st, 2012, 04:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your template is doing apply-templates to process attribute nodes but you no longer have a template rule that handles attribute nodes. Replace the

Code:
<xsl:apply-templates select="@*|node()"/>
with

Code:
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
madowl (March 6th, 2012)
 
Old March 6th, 2012, 12:07 AM
Authorized User
 
Join Date: Feb 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Hi Michael,
I am running into this error upon transformation. There are certain elements like this:
<EmailAddress xsi:nil="true"></EmailAddress>
When such elements are encountered the xslt is throwing the error:
Undeclared namespace prefix in DOM input: xsi
I want to keep these elements but disregard the xsi:nil="true". In other words this should be <EmailAddress /> or <EmailAddress> </EmailAddress>
WHat would you recommend in this case? My code is as follows:
Code:
<xsl:stylesheet version="2.0"
                xmlns:v="http://vocado.vsm-systems.com/xsd/vocado-tx"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:default="http://www.ed.gov/FSA/COD/2011/v3.0e" exclude-result-prefixes="default">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <!--Define the latest name space-->

    <xsl:variable name="ns" select='"http://www.ed.gov/FSA/COD/2011/v3.0e"'/>
    <!-- This template will plug the latest name space in -->
    <xsl:template match="*">
        <xsl:element name="{local-name()}" namespace="{$ns}">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
 
Old March 6th, 2012, 04:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

One of the unfortunate things about DOM (one of many...) is that it allows you to construct trees that do not represent well-formed XML documents. XSLT will only handle input that is well-formed (and namespace-well-formed). If you give it a DOM that contains namespace prefixes with no corresponding declarations, it will choke.

You probably won't like my recommendation but I will give it anyway: stop using DOM. Ideally, do all your XML manipulation in high level languages - XSLT and XQuery. If you can't achieve that and need to use Java, then use one of the more modern tree representations such as JDOM or XOM.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old March 6th, 2012, 10:40 AM
Authorized User
 
Join Date: Feb 2012
Posts: 13
Thanks: 4
Thanked 0 Times in 0 Posts
Default

Hi Michael, I see what you are saying and it makes sense from a technical standpoint. However, DOM is used throughout the application and hence am kind of stuck using it





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in replacing XML namespace using XSLT kris13 XSLT 16 December 23rd, 2009 05:43 AM
error CS0234: The type or namespace name 'Xml' does not exist in the namespace 'Syste shailesh_kumar C# 2008 aka C# 3.0 8 August 20th, 2009 03:11 AM
Need to convert an existing namespace and add new namespace to the SOAP xml Prabeen XSLT 10 April 28th, 2009 10:18 AM
Replacing nbsp delinear Javascript 2 February 5th, 2005 12:22 PM





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