且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 XSLT 从 s-s-rS/Visual Studio 导出到 XML

更新时间:2022-12-31 08:01:15

您的输入 XML 位于命名空间中,即默认命名空间:

Your input XML is in a namespace, that is, a default namespace:

<Report xmlns="testreport">

所有后代元素也将属于这个命名空间.在您的情况下,重要的是元素 Report 和名为 {testreport}Report 的元素({} 告诉您存在命名空间) 是完全不同的元素.

All descendant elements will also belong to this namespace. What matters in your case is that the element Report and the one called {testreport}Report (the {} tell you that a namespace is present) are entirely different elements.

所以,如果您的模板匹配

So, if your template matches

<xsl:template match="Report">

只有在没有命名空间的名为Report"的元素被发现时才会触发.反而,如果您需要处理特定元素的名称并且如果它们位于命名空间中,则需要在 XSLT 样式表中声明此命名空间并为这些元素名称添加前缀:

it is only triggered if an element named "Report" is found which is in no namespace. Instead, if you need to address the names of specific elements and if they are in a namespace, you need to declare this namespace in an XSLT stylesheet and prefix those element names:

<xsl:stylesheet xmlns:tst="testreport">
  <xsl:template match="tst:Report">
    <!--...-->
  </xsl:template>
  <!--...-->
</xsl:stylesheet>

你观察到

似乎正在工作的元素的唯一模板匹配是 "/""*""/*">

the only template matches for elements that seem to be working are "/", "*" and "/*"

因为这些模式是通用的,不使用需要前缀的特定元素名称.

because those patterns are generic ones that do not make use of specific element names that would need prefixing.

XSLT 样式表

更多细节:无需在样式表中重新声明 xsi 命名空间 - 我已将其省略.此外,一些处理器保留空白节点,导致输出格式错误 - 我添加了 xsl:strip-space 来解决这个问题.

More details: No need for redeclaring the xsi namespace in the stylesheet - I have omitted it. Also, some processors keep whitespace nodes which results in the output being badly formatted - I have added xsl:strip-space to account for this.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:tst="testreport"
    exclude-result-prefixes="tst">
  <xsl:output method="xml" indent="yes" encoding="utf-8" />

  <xsl:strip-space elements="*"/>

  <!-- rule to suppress the undesired nodes -->
  <xsl:template match="tst:Report|tst:ProductList|tst:Details_Collection">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- rule to rename the Details node -->
  <xsl:template match="tst:Details">
    <item>
      <xsl:apply-templates/>
    </item>
  </xsl:template>

  <!-- rule to copy everything else -->
  <xsl:template match="*|@*">
    <xsl:element name="{name()}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <!-- rule to start the conversion and provide the wrapper tags -->
  <xsl:template match="/">
    <rss version="2.0">
      <channel>
        <xsl:apply-templates/>
      </channel>
    </rss>
  </xsl:template>

</xsl:stylesheet>

XML 输出

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
   <channel>
      <item>
         <ID>602</ID>
         <Title>302</Title>
      </item>
      <item>
         <ID>603</ID>
         <Title>303</Title>
      </item>
   </channel>
</rss>