XSLT:在模板中的两个元素之间插入新行

XSLT : Insert new line between two elements in a template

我目前正在进入 XSLT 的世界,因为我必须在两个 XML 文件之间进行转换。

我开始了,但我遇到了一个小问题,这使得我的文件在每一代上都难以读取。

我输入了一个 XML,它是:

<?xml version="1.0" encoding="utf-8"?>
<ConfigurationNodes>
   <Versions>
      <PackagesA Version="1.8" />
      <PackageB Version="1.1" />
      <PackageC Version="1.7" />
   </Versions>
   <ConfigurationNode  Type="SomeSpecialType">
      <Name>MyName</Name>
      <Revision>0</Revision>
      <Description >The big full description here</Description>
   </ConfigurationNode>
</ConfigurationNodes>

我目前对其应用了以下转换:

<?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 method="xml" indent="yes" />

  <xsl:template match="ConfigurationNode[@Type='SomeSpecialType']">
    <Object Name="Configuration" NodeType="SomeOtherType">
      <Property Name="Name" Value="{Name/text()}" Type="System.String"/>
      <Property Name="Description" Value="{Description/text()}" Type="System.String"/>
      <Property Name="Revision" Value="{Revision/text()}" Type="System.Int32" />
    </Object>
  </xsl:template>


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

这项工作,但目前我的 Property 个元素在同一行上(在我的真实情况下,我在这里有 10 个 属性):

<?xml version="1.0" encoding="utf-8"?>
<ConfigurationNodes>
   <Versions>
      <PackagesA Version="1.8" />
      <PackageB Version="1.1" />
      <PackageC Version="1.7" />
   </Versions>
   <Object Name="Configuration" NodeType="SomeOtherType"><Property Name="Name" Value="MyName" Type="System.String" /><Property Name="Description" Value="The big full description here" Type="System.String" /><Property Name="Revision" Value="0" Type="System.Int32" /></Object>
</ConfigurationNodes>

我的目标是

<?xml version="1.0" encoding="utf-8"?>
<ConfigurationNodes>
   <Versions>
      <PackagesA Version="1.8" />
      <PackageB Version="1.1" />
      <PackageC Version="1.7" />
   </Versions>
   <Object Name="Configuration" NodeType="SomeOtherType">
      <Property Name="Name" Value="MyName" Type="System.String" />
      <Property Name="Description" Value="The big full description here" Type="System.String" />
      <Property Name="Revision" Value="0" Type="System.Int32" />
   </Object>
</ConfigurationNodes>

经过一些研究,我尝试放置一个 <xsl:preserve-space elements="*"/>,但没有成功。

我到处都看到有人遇到相反的问题(太多 space),但我没有找到与我遇到的问题相同的问题。

我想您已经猜到我正在 Visual studio(msxsl namespace).

中进行此转换

我没有要测试的 MSXML 处理器,但我使用 libxslt 处理器设法产生了同样的问题。在这里,可以通过添加解决问题:

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

到样式表的顶层。顺便说一句,如果您使用的是 identity transform 模板,那么几乎总是应该包括在内。