<>I>。 此处为一种通用的参数XSLT1.0解决办法:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pElemName" select=" hel:docTypeRef_tns_sayHello "/>
<xsl:param name="pOldNamespace" select=" http://aaa/bbb.fr "/>
<xsl:param name="pNewNamespace" select=" http://test.fr "/>
<xsl:variable name="vPrefix" select="substring-before($pElemName, : )"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test=
"not(name() = $pElemName
and
namespace-uri() = $pOldNamespace
)
">
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{name()}" namespace="{$pNewNamespace}">
<xsl:copy-of select=
"namespace::*
[not(name() = $vPrefix
and
. = $pOldNamespace
)
]"/>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()" mode="removeNS"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="removeNS">
<xsl:choose>
<xsl:when test=
"not(starts-with(name(), $vPrefix)
and
namespace-uri() = $pOldNamespace
)">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select=
"namespace::*
[not(name() = $vPrefix
and
. = $pOldNamespace
)
]"/>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()" mode="removeNS"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="@mustUnderstand">
<xsl:attribute name="{name()}">0</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
当这一转变适用于XML文件时:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<soapenv:Header>
<wsse:Security mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>test</wsse:Username>
<wsse:Password>test</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<hel:docTypeRef_tns_sayHello xmlns:hel="http://aaa/bbb.fr">
<arg0>John</arg0>
<arg1>111-222-333</arg1>
</hel:docTypeRef_tns_sayHello>
</soapenv:Body>
</soapenv:Envelope>
<t, 正确结果::
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<soapenv:Header>
<wsse:Security mustUnderstand="0">
<wsse:UsernameToken>
<wsse:Username>test</wsse:Username>
<wsse:Password>test</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<hel:docTypeRef_tns_sayHello xmlns:hel="http://test.fr">
<arg0>John</arg0>
<arg1>111-222-333</arg1>
</hel:docTypeRef_tns_sayHello>
</soapenv:Body>
</soapenv:Envelope>
xsl:名称指示可用于动态构建动态(法定身份不明)部件的新名称空间。
Here is a small example how with XSLT 1.0 to add a new, non-existent at the start of the transformation namespace node:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:variable name="vrtfDoc">
<t xmlns:hel="http://test.fr"/>
</xsl:variable>
<xsl:variable name="vNS" select=
"ext:node-set($vrtfDoc)/*/namespace::*
[name()= hel ]"/>
<xsl:element name="{name()}"
namespace="{namespace-uri()}">
<xsl:copy-of select="namespace::*"/>
<xsl:copy-of select="$vNS"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
当这一转变适用于本十份LL文件时:
<t xmlns:someNS="some:NS"/>
它重新编号为<代码>t及其所有名称空间节点,并在其中添加一个新名称空间节点:
<t xmlns:someNS="some:NS" xmlns:hel="http://test.fr"/>
www.un.org/Depts/DGACM/index_spanish.htm II. 这里是一个完整的XSLT 2.0解决方案:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pElemName" select=" hel:docTypeRef_tns_sayHello "/>
<xsl:param name="pOldNamespace" select=" http://aaa/bbb.fr "/>
<xsl:param name="pNewNamespace" select=" http://test.fr "/>
<xsl:variable name="vPrefix" select="substring-before($pElemName, : )"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:namespace name="{$vPrefix}" select="$pNewNamespace"/>
<xsl:copy-of select=
"namespace::*
[not(name() = $vPrefix
and
. = $pOldNamespace
)
]"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test=
"not(name() = $pElemName
and
namespace-uri() = $pOldNamespace
)
">
<xsl:call-template name="identity"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{name()}" namespace="{$pNewNamespace}">
<xsl:copy-of select=
"namespace::*
[not(name() = $vPrefix
and
. = $pOldNamespace
)
]"/>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()" mode="removeNS"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="removeNS">
<xsl:choose>
<xsl:when test=
"not(starts-with(name(), $vPrefix)
and
namespace-uri() = $pOldNamespace
)">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select=
"namespace::*
[not(name() = $vPrefix
and
. = $pOldNamespace
)
]"/>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()" mode="removeNS"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="@mustUnderstand">
<xsl:attribute name="{name()}">0</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the same XML document (above), the wanted, correct result is produced:
<soapenv:Envelope xmlns:hel="http://test.fr"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<soapenv:Header>
<wsse:Security mustUnderstand="0">
<wsse:UsernameToken>
<wsse:Username>test</wsse:Username>
<wsse:Password>test</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<hel:docTypeRef_tns_sayHello>
<arg0>John</arg0>
<arg1>111-222-333</arg1>
</hel:docTypeRef_tns_sayHello>
</soapenv:Body>
</soapenv:Envelope>