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

J4N*_*J4N 3 xml xslt whitespace visual-studio-2010

我目前正在进入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>
Run Code Online (Sandbox Code Playgroud)

我目前正在应用以下转换:

<?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>
Run Code Online (Sandbox Code Playgroud)

这项工作,但目前我的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>
Run Code Online (Sandbox Code Playgroud)

我的目标是拥有

<?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>
Run Code Online (Sandbox Code Playgroud)

经过一番研究,我试图放一个<xsl:preserve-space elements="*"/>,没有运气.

我到处都看到有相反问题的人(空间太大),但我找不到与我有相同的问题.

我想你已经猜到我在Visual Studio(msxsl命名空间)中进行这种转换.

mic*_*57k 7

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

<xsl:strip-space elements="*"/>
Run Code Online (Sandbox Code Playgroud)

到样式表的顶层.如果您使用身份转换模板,则应该几乎总是包含此BTW .