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 25th, 2007, 09:17 AM
Registered User
 
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to match attributes xmlns:tns etc


Hi,

Is it possible to match the attributes that declare namespaces?

In this example I want all to replace the value "someURI" with "otherURI" in all attributes. However, the xmlns:other attribute is not affected. Why?

Im using Windows XP with Saxon 8.7.3.

---------
test.xsl:
---------
<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@*[contains(.,'someURI')]">
    <xsl:variable name="tmp">
        <xsl:value-of select="name(.)"/>
    </xsl:variable>
    <xsl:attribute name="{$tmp}">
        <xsl:value-of select="replace(.,'someURI','otherURI')"/>
    </xsl:attribute>

</xsl:template>

</xsl:stylesheet>

--------
test.xsd
--------
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"
    targetNamespace="someURI/tmp1"
    xmlns:tns="someURI/tmp1"
    xmlns:other="someURI/tmp2">

    <xs:import
        namespace="someURI/tmp2"
        schemaLocation="model_1_0.xsd" />
</xs:schema>

----------------
wantedOutput.xsd
----------------
<?xml version='1.0' ?>
<xs:schema xmlns:tns="someURI/tmp1"
    targetNamespace="otherURI/tmp1"
    xmlns:other="otherURI/tmp2"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:import namespace="otherURI/tmp2" schemaLocation="model_1_0.xsd"/>

</xs:schema>

----------------
actualOutput.xsd
----------------
<?xml version='1.0' ?>
<xs:schema xmlns:tns="someURI/tmp1"
    targetNamespace="otherURI/tmp1"
    xmlns:other="someURI/tmp2"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:import namespace="otherURI/tmp2" schemaLocation="model_1_0.xsd"/>

</xs:schema>

Best Regards
-Thomas

 
Old February 25th, 2007, 09:19 AM
Registered User
 
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Oops, the wanted output should of course be:

----------------
wantedOutput.xsd
----------------
<?xml version='1.0' ?>
<xs:schema xmlns:tns="otherURI/tmp1"
    targetNamespace="otherURI/tmp1"
    xmlns:other="otherURI/tmp2"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:import namespace="otherURI/tmp2" schemaLocation="model_1_0.xsd"/>

</xs:schema>

Sorry about that.

 
Old February 25th, 2007, 10:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In the XPath data model, namespace declarations are represented as namespace nodes, not as attribute nodes. You can select them using the namespace axis rather than the attribute axis. In XSLT 2.0 you can create a new namespace node using the xsl:namespace instruction (rather than the xsl:attribute instruction).

However, changing the namespace nodes will not have the effect of changing the expanded names of any elements or attributes. Remember than in the data model, the name of an element or attribute has two parts, the local name and the namespace URI, and if you want to change either part you need to do so explicitly, for example by

<xsl:template match="*">
  <xsl:element name="{local-name()}" namespace="new-namespace.uri">



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I get rid of xmlns= attributes? Peter Savas XSLT 6 July 11th, 2007 03:33 PM
template match doesnt match the required node Tomi XSLT 2 March 12th, 2007 06:24 AM
XMLNS mathias XML 0 February 20th, 2007 04:46 AM
TNS protocol adapter error !!!!!! nitin garg Oracle 2 April 5th, 2006 11:29 PM
xmlns albusr .NET Web Services 0 December 24th, 2005 08:25 AM





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