Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xslt转换期间丢失xml标记_Xml_Xslt_Xslt 1.0_Transformation_Apply Templates - Fatal编程技术网

使用xslt转换期间丢失xml标记

使用xslt转换期间丢失xml标记,xml,xslt,xslt-1.0,transformation,apply-templates,Xml,Xslt,Xslt 1.0,Transformation,Apply Templates,在使用.xsl样式表转换xml文件的过程中,重命名子节点时遇到问题。问题是,只处理值,而不处理标记。我两者都想要 改造前的原件: <old> <ns2:Header> <EntityId>yxc</EntityId> <Application>11</Application> <Version>354</Version> <User> <Id>user

在使用.xsl样式表转换xml文件的过程中,重命名子节点时遇到问题。问题是,只处理值,而不处理标记。我两者都想要

改造前的原件:

<old>
 <ns2:Header>
  <EntityId>yxc</EntityId>
  <Application>11</Application>
  <Version>354</Version>
  <User>
    <Id>user1</Id>
  </User>
</ns2:Header>
 ....
 </old>
预期结果:

<new>
  <Header>
  <EntityId>yxc</EntityId>
  <Application>11</Application>
  <Version>354</Version>
  <User>
    <Id>user1</Id>
  </User>
 </Header>
 ...
</new>
到目前为止,我得到的是这样的.xsl:

<?xml version="1.0" encoding="UTF-8"?>
   <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml"/>

    <xsl:template match="/">
      <new xmlns:ns2="http://example.com">

      <xsl:template match="//ns2:Header">
            <xsl:element name="Header">
                <xsl:apply-templates select="//ns2:Header" />
            </xsl:element>
        </xsl:template>
       .....
        </new>
     </xsl:template>
  </xsl:stylesheet>
转换后,Header的子节点丢失,只有值仍然存在:

<new xmlns:ns2="http://example">
    <Header>
       yxc
       11
       354

        user1
     </Header>
     ...
</new>
我想我缺少了一些关于apply templates函数的表达式。 有什么想法吗


谢谢

您可能没有松开节点。若您使用w3c xslt tester在浏览器中显示转换后的xml,那个么它可能只是UI表示。这是我的代码。运行下面的脚本并单击transform

这是我的xsl,用于以下测试片段:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:template match="/">
      <new xmlns:ns2="http://example.com">
        <Header>
          <xsl:copy-of select="//*[local-name()='ns2:Header']/*" />
        </Header>
      </new>

    </xsl:template>

  </xsl:stylesheet>
要转换的XML: yxc 11 354 用户1 XSL: 转换xml:
问题在于,仅应用模板而不定义模板不会复制选定的节点。在这种情况下会执行一些内置模板,其中一个是:

<xsl:template match="text()|@*">
   <xsl:value-of select="."/>
</xsl:template>
这就是为什么您在输出中看到文本值,而不是它们周围的标记。

第1部分-通常不正确的地方是什么 如果在XML文件中使用任何名称空间,则应定义该名称空间, e、 g.在主标签中。因此,主标签应该是:

<old xmlns:ns2="http://example.com">
由于您的脚本可能不包含此类标识模板,因此XSLT 处理器应用其默认规则,仅复制内容

第3部分-如何完成此任务 如我所见,您需要进行两项更改:

删除标头标记中包含的命名空间。 将旧标记名更改为新标记名。 要删除任何名称空间,我们必须替换标识模板 使用以下两个模板处理元素和属性:

<xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@*">
  <xsl:attribute name="{local-name()}">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>
因此,整个脚本可以编写如下:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*"/>

  <!-- Change element name -->
  <xsl:template match="old">
    <new>
      <xsl:apply-templates/>
    </new>
  </xsl:template>

  <!-- Copy elements w/o namespace -->
  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <!-- Copy attributes w/o namespace -->
  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

</xsl:transform>

我添加了条带空间以从输出中删除其他空行。

您的输入XML当前无效。它使用的是ns2:namespace前缀,但没有使用该前缀声明名称空间URL。您的输入XML中是否缺少xmlns:ns2=“??”?谢谢
<xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@*">
  <xsl:attribute name="{local-name()}">
    <xsl:value-of select="."/>
  </xsl:attribute>
</xsl:template>
<xsl:template match="old">
  <new>
    <xsl:apply-templates/>
  </new>
</xsl:template>
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:strip-space elements="*"/>

  <!-- Change element name -->
  <xsl:template match="old">
    <new>
      <xsl:apply-templates/>
    </new>
  </xsl:template>

  <!-- Copy elements w/o namespace -->
  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <!-- Copy attributes w/o namespace -->
  <xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

</xsl:transform>