XSL:删除命名空间前缀 [英] XSL: Remove namespace prefix

查看:51
本文介绍了XSL:删除命名空间前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了一段时间后,我想我会在这里问.我正在尝试从 XSL 输出文件中删除空命名空间.让我提供相关代码:

After searching for a while i figured I'd ask here. I'm trying to remove the empty namespace from an XSL output file. Let me provide the relevant code:

输入 XML

<?xml version="1.0" encoding="UTF-8"?>
<DK_RequestChangeOfSupplier xmlns="un:unece:260:data:EEM-DK_RequestChangeOfSupplier:v2">
    <HeaderEnergyDocument>
        <Creation>Date</Creation>
    </HeaderEnergyDocument>
</DK_RequestChangeOfSupplier>

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:ns1="un:unece:260:data:EEM-DK_RequestChangeOfSupplier:v2"
            exclude-result-prefixes="ns1">

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

    <xsl:template match="/">
        <xsl:for-each select="ns1:DK_RequestChangeOfSupplier">
            <McsDkSMBMsg 
             xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/McsDkSMBMsg">
                <xsl:call-template name="CreateElement">
                    <xsl:with-param name="ElementName" select="'Creation'" />
                </xsl:call-template>
            </McsDkSMBMsg>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="CreateElement">
        <xsl:param name="ElementName"></xsl:param>
        <xsl:element name="{$ElementName}">Testing</xsl:element>
    </xsl:template>
</xsl:stylesheet>

输出

<McsDkSMBMsg xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/McsDkSMBMsg">
    <Creation xmlns="">Testing</Creation>
</McsDkSMBMsg>

如何删除命名空间?我是不是在做一些完全错误的 XSL 明智的事情?我慢慢地把头发拉出来.问候.

How do I remove the namespace? Am I doing something completely wrong XSL-wise? I'm slowly pulling my hair out. Regards.

推荐答案

您的原始代码向输出树添加了两个元素:一个名为 McsDkSMBMsg,它具有 默认命名空间:

Your original code adds two elements to the output tree: One called McsDkSMBMsg which has a default namespace:

<McsDkSMBMsg xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/McsDkSMBMsg">

另一个是 Creation - 根据您的原始代码,它根本没有命名空间.XSLT 处理器确保 Creation 元素不会出现在默认命名空间中的唯一方法是取消声明这个命名空间:

the other one is Creation - which, according to your original code, is in no namespace at all. The only way for the XSLT processor to ensure that the Creation element does not end up in the default namespace is to undeclare this namespace:

<Creation xmlns="">Testing</Creation>

我假设您希望 Creation 元素也属于该默认命名空间.如果是这样,只需将相同的 xmlns 属性添加到创建 Creationxsl:element 指令.XLST 处理器将处理任何冗余的命名空间声明.

I assume you'd like the Creation element to also belong to that default namespace. If so, simply add the same xmlns attribute to the xsl:element instruction where Creation is created. The XLST processor will take care of any redundant namespace declarations.

样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:ns1="un:unece:260:data:EEM-DK_RequestChangeOfSupplier:v2"
            exclude-result-prefixes="ns1">

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

    <xsl:template match="/">
        <xsl:for-each select="ns1:DK_RequestChangeOfSupplier">
            <McsDkSMBMsg 
             xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/McsDkSMBMsg">
                <xsl:call-template name="CreateElement">
                    <xsl:with-param name="ElementName" select="'Creation'" />
                </xsl:call-template>
            </McsDkSMBMsg>
        </xsl:for-each>
    </xsl:template>

    <xsl:template name="CreateElement">
        <xsl:param name="ElementName"></xsl:param>
        <xsl:element name="{$ElementName}" xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/McsDkSMBMsg">Testing</xsl:element>
    </xsl:template>
</xsl:stylesheet>

XML 输出

现在 xmlns 属性仅出现在 McsDkSMBMsg 元素上.

Now an xmlns attribute is only present on the McsDkSMBMsg element.

<?xml version="1.0" encoding="UTF-8"?>
<McsDkSMBMsg xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/McsDkSMBMsg">
   <Creation>Testing</Creation>
</McsDkSMBMsg>

<小时>

也就是说,也许您的代码需要一些改进.您正在将 for-each 应用于最外层元素 - 其中只能有一个 - 这是没有意义的.此外,为新命名空间定义一个前缀可能更合理.并且您无缘无故地调用了一个命名模板.


That said, perhaps your code could need some improvement. You are applying a for-each to the outermost element - of which there can only be one - which makes no sense. Also, it might be more reasonable to define a prefix for the new namespace. And you are calling a named template for no obvious reason.

它的用处取决于实际的 XML 输入和您已有的其他代码.

The usefulness of it depends on the actual XML input and on other code you already have.

改进的样式表

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:ns1="un:unece:260:data:EEM-DK_RequestChangeOfSupplier:v2"
            xmlns:msg="http://schemas.microsoft.com/dynamics/2008/01/documents/McsDkSMBMsg"
            exclude-result-prefixes="ns1">

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

    <xsl:template match="ns1:DK_RequestChangeOfSupplier">
            <msg:McsDkSMBMsg>
                <xsl:element name="msg:Creation">Testing</xsl:element>
            </msg:McsDkSMBMsg>
    </xsl:template>

</xsl:stylesheet>

这篇关于XSL:删除命名空间前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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