Multiple namespaces for an element with XSLT 1.0

I use only the Microsoft XSLT processor (only 1.0)

XML Opening Lines:

<?xml version="1.0" encoding="utf-8"?>
<Header xmlns="http:\\OldNameSpace.com">
    <Detail>

Have the following XSLT template to select an element of <Header>my document and change its namespace.

<xsl:template match="*">
    <xsl:element name="{name()}" xmlns="http:\\NewNameSpace.com"> 
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

Which turns <Header xmlns="http:\\OldNameSpace.com">into<Header xmlns="http:\\NewNameSpace.com">

However, now I need to add a second namespace to it in order to get the following output:

<Header xmlns="NewNameSpace.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

I tried using:

<xsl:template match="*">
    <xsl:element name="{name()}" xmlns="NewNameSpace.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates />
    </xsl:element>
</xsl:template>

However, I still get the same result as the original XSLT template.

Can anyone tell me why this is?

+3
source share
3 answers

This conversion is :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:old="http:\\OldNameSpace.com"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  exclude-result-prefixes="old xsi">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pNewNamespace" select="'http:\\NewNameSpace.com'"/>

 <xsl:variable name="vXsi" select="document('')/*/namespace::*[name()='xsi']"/>

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

    <xsl:template match="old:*">
        <xsl:element name="{local-name()}" namespace="{$pNewNamespace}">
          <xsl:copy-of select="$vXsi"/>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

when applied to the following XML document :

<Header xmlns="http:\\OldNameSpace.com">
    <Detail/>
</Header>

creates (what I think) the desired, correct result :

<Header xmlns="http:\\NewNameSpace.com"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Detail/>
</Header>
+5

xsl: element ( ) , , 9, , ).

xslt2 xsl: namespace , xslt1

  

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

- (, xsl: stylesheet.)

xsi:tmp="" , , , , xsi:type tmp , . , , xsi, . , msxsl:node-set .

+2

, , XSLT 1.0 - xsl: copy-of. <dummy xmlns:xsi="http://whatever"/>, <xsl:copy-of select="document('dummy.xml')/*/namespace::xsi"/> xsl:element.

+2
source

All Articles