使用XSLT Transformation在XML中创建xmlns属性 [英] Create xmlns attribute in the XML using XSLT Transformation

查看:79
本文介绍了使用XSLT Transformation在XML中创建xmlns属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JDK Transformer(Oracle XML v2 Parser或JAXP)在XSLT转换期间使用参数传递的值将xmlns属性添加到生成的XML中,但它始终默认为 http://www.w3.org/2000/xmlns/

I am trying to add the xmlns attribute to the resulting XML with a value passed by parameter during XSLT transformation using JDK Transformer (Oracle XML v2 Parser or JAXP) but it always defaults to http://www.w3.org/2000/xmlns/

我的源XML

<test/>

我的XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://example.com">
    <xsl:param name="myNameSpace" select="'http://neilghosh.com'"/>
    <xsl:template match="/">
        <process>
            <xsl:attribute name="xmlns:neil">
                <xsl:value-of select="$myNameSpace"/>
            </xsl:attribute>
        </process>
    </xsl:template>
</xsl:stylesheet>

我的结果

<?xml version="1.0"?>
<process xmlns="http://www.w3.org/2000/xmlns/" xmlns:neil="neilghosh.com">
</process>

我想要的结果

<?xml version="1.0"?>
<process xmlns="http://example.com"  xmlns:neil="neilghosh.com">
</process>


推荐答案

首先,在XSLT数据模型中,你不要如果要创建属性节点,则需要创建命名空间节点。

Firstly, in the XSLT data model, you don't want to create an attribute node, you want to create a namespace node.

命名空间节点通常是自动创建的:如果在特定命名空间中创建元素或属性,必需的命名空间节点(因此,当序列化时,命名空间声明)由处理器自动添加。

Namespace nodes are usually created automatically: if you create an element or attribute in a particular namespace, the requisite namespace node (and hence, when serialized, the namespace declaration) are added automatically by the processor.

如果要创建不必要的命名空间节点(因为它没有在任何元素或属性的名称中使用),那么在XSLT 2.0中,您可以使用xsl:namespace。如果你坚持使用XSLT 1.0,那么就有一种解决方法,包括在相关命名空间中创建一个元素,然后复制其命名空间节点:

If you want to create a namespace node that isn't necessary (because it's not used in the name of any element or attribute) then in XSLT 2.0 you can use xsl:namespace. If you're stuck with XSLT 1.0 then there's a workaround, that involves creating an element in the relevant namespace and then copying its namespace node:

<xsl:variable name="ns">
  <xsl:element name="neil:dummy" namespace="{$param}"/>
</xsl:variable>
<process>
  <xsl:copy-of select="$ns/*/namespace::neil"/>
</process>

这篇关于使用XSLT Transformation在XML中创建xmlns属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆