使用 XSL 向 XML 文档添加 xmlns 属性的问题 [英] Problem with adding xmlns attribute to XML document using XSL

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

问题描述

我的 XSL 中有以下内容,它向我的 XML 添加了 xmlns.

I have the following in my XSL which adds a xmlns to my XML.

<xsl:template match="root">
    <xsl:element name="root" namespace="myXslLoc">
        <xsl:attribute name="Name">Default</xsl:attribute>
        <xsl:apply-templates/>
    </xsl:element>
<xsl:template>

上面确实向根元素(顶级元素)添加了 xmlns 属性.但是,它还向后续元素添加了 xmlns.结果是这样的:

The above does add a xmlns attribute to the root element (the top level element). However it also added a xmlns to the subsequent element. This is how it turned out:

<root Name="Default" xmlns="myXslLoc">
    <steps xmlns="">  <-- where did this attribute come from?
    .
    .
    .
    </steps>
</root>

我不知道步骤元素中的 xmlns 来自哪里.我没有指定将 xmlns 添加到步骤元素的代码.下面是我的 xsd 的片段:

I have no ideas where that xmlns in the steps element come from. I have no code that specifies the adding of the xmlns to the steps element. Below is a snipet of my xsd:

<xs:complexType name="root">
    <xs:sequence>
        <xs:element name="steps" type="steps" maxOccurs="1" MinOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string" use="required"/>
</xs:complexType>

<xs:complexType name="steps">
    <xs:sequence>
        <xs:element name="step" type="step" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:complexType>

我的 xsl 或 xsd 有什么问题吗?我似乎无法弄清楚问题出在哪里.

Is there something wrong in my xsl or xsd? I can not seem to figure out where the problem came from.

谢谢.

按照 Dimitre 的转换代码,我设法将命名空间属性插入到根元素中.然而,更多的命名空间属性实例出现在转换后的 xml 文档中.

Following Dimitre's transformation code, I have managed to insert the namespace attribute into the root element. However, more instances of namespace attribute appeared further down in the transformed xml document.

下面是发生的事情:

<root Name="Default" xmlns="myXslLoc">
    <steps>  <-- where did this attribute come from?
        <step name="A">
        .
        </step>
        .
        .
        <!-- the below is the final steps element -->
        <step name="Z" xmlns="">  <-- this xmlns was unexpectedly added.
            <steps xmlns="myXslLoc"> <-- this one as well.
            .
            .
            .
            </steps>
        </step>
        <step Name="Step manually added by identity transformation (addLastNode stuff)">
        .
        .
        .
        </step>
    </steps>
</root>

xsl 看起来像这样:

The xsl looks something like this:

<xsl:template match="root">
    <xsl:element name="root namespace="myXslLoc">
        <xsl:attribute name="Name">Default</xsl:attribute>
        <xsl:apply-templates/>
    </xsl:element>
<xsl:template>

<xsl:template match="*">
    <xsl:element name="{name()}" namespace="{$addMyXslLoc}">
        <xsl:copy-of select="namespace::*"/>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
<xsl:template>

<xsl:param name="addMyXslLoc" select="'myXslLoc'"/>

<xsl:template match="/*/steps/step[position()=last()]">
    <xsl:call-template name="identity"/>
    <xsl:copy-of select="$addLastNodes"/>
</xsl:template>

<xsl:param name="addLastNodes">
    <step Name="Total Cost">
        <items>
            <item name="A">
            </item>
            <item name="b">
            </item>
        </items>
    </step>
</xsl:param>

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

我喜欢命名空间现在出现在根元素上的方式(以及 Name 属性).但是我现在面临的问题是无法摆脱插入到 xml 文档最后一个元素中的命名空间,不包括通过转换添加的那个.

I liked how the namespace now appears on the root element (along with the Name attribute). But I now face the problem of unable to get rid of the namespaces which was inserted into the last element of the xml document excluding the one that is added via the transformation.

更新了 addLastNodes xsl.

Updated the addLastNodes xsl.

推荐答案

@Vincent-Marchetti 已经解释了导致问题的原因.这是一个完整的解决方案.

@Vincent-Marchetti has already explained what is causing the problem. Here is a complete solution.

这种转变:

<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:strip-space elements="*"/>

 <xsl:param name="pNamespace" select="'myXslLoc'"/>

  <xsl:param name="addLastNodes">
    <step Name="Total Cost">
        <items>
            <item name="A">
            </item>
            <item name="b">
            </item>
        </items>
    </step>
 </xsl:param>


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

  <xsl:template match="/*/steps/step[position()=last()]">
    <xsl:call-template name="repNamespace"/>
    <xsl:apply-templates select="ext:node-set($addLastNodes)/*"/>
 </xsl:template>

 <xsl:template match="*" name="repNamespace">
  <xsl:element name="{name()}" namespace="{$pNamespace}">
    <xsl:copy-of select="namespace::*"/>
    <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

应用于此 XML 文档时:

<root>
 <steps>
  <step>1</step>
  <step>2</step>
  <step>3</step>
 </steps>
</root>

产生想要的、正确的结果:

<root xmlns="myXslLoc">
   <steps>
      <step>1</step>
      <step>2</step>
      <step>3</step>
      <step Name="Total Cost">
         <items>
            <item name="A"/>
            <item name="b"/>
         </items>
      </step>
   </steps>
</root>

这篇关于使用 XSL 向 XML 文档添加 xmlns 属性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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