0

我有一个 XML 需要重新排序并存储在另一个文件中。我为此做了一个xslt,它工作正常。但是,如果 xml 结束后有注释,则不会复制这些注释。我需要一个 xslt 语句来复制根标记结束后出现的注释下面是解释以下内容的代码

原始 XML

    <Company>
      <Employee id="100" Name="John" >
        <Salary value="15000"/>
        <Qualification text="Engineering">
        <State name="Kerala" code="02">
        <Background text="Indian">
      </Employee>
    </Company>

<!--This file contains Employee information-->
<!--Please refer the file to get information about an employee-->

XSLT 转换代码

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output indent="yes"  omit-xml-declaration="yes" method="xml" />
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="Employee">
    <xsl:copy>
      <xsl:apply-templates select="Qualification"/>
      <xsl:apply-templates select="Salary" />
      <xsl:apply-templates select="Background"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

获得的输出

<?xml version="1.0" encoding="utf-8"?>
<Company>
  <Employee>
    <Qualification text="Engineering" />
    <Salary value="15000" />
    <Background text="Indian" />
  </Employee>
</Company>

需要输出

<?xml version="1.0" encoding="utf-8"?>
<Company>
  <Employee>
    <Qualification text="Engineering" />
    <Salary value="15000" />
    <Background text="Indian" />
   </Employee>
</Company>

<!--This file contains Employee information-->
<!--Please refer the file to get information about an employee-->
4

1 回答 1

0

你的转变没有任何问题。我在相同的 XML 上运行了相同的 XSLT(已更正以使其格式正确),并且我得到了正确的输出,包括尾随注释(尽管由于样式表中的和 ,xsltproc它们丢失了原始缩进和间距)strip-spaceindent="yes"

<Company>
  <Employee>
    <Qualification text="Engineering"/>
    <Salary value="15000"/>
    <Background text="Indian"/>
  </Employee>
</Company><!--This file contains Employee information-->
<!--Please refer the file to get information about an employee-->

看起来您正在使用的任何处理器或 XML 解析器(可能是 Microsoft 判断的xmlns:msxsl="urn:schemas-microsoft-com:xslt")都忽略了文档元素结束标记之后的注释。

于 2013-07-31T12:13:58.793 回答