Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT : Insert new line between two elements in a template

I'm currently entering in the world of XSLT, because I've to make a transformation between two XML files.

I'm starting, but I face a small issue which make my files hardly readable on every generation.

I've as input an XML which is:

<?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>

On which I currently apply the following transformation:

<?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>

This work, but currently my Property elements are on the same line(and in my real case, I've 10 property here):

<?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>

My goal is to have

<?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>

After some research, I've tried to put an <xsl:preserve-space elements="*"/>, with no luck.

I read everywhere people having the opposite issue(too much space), but I didn't find the same issue that I'm having.

I think you already guessed that I'm doing this transformation within Visual studio(msxsl namespace).

like image 767
J4N Avatar asked Oct 15 '25 15:10

J4N


1 Answers

I don't have a MSXML processor to test with, but I managed to produce the same problem using the libxslt processor. Here, the issue can be solved by adding:

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

to the top level of the stylesheet. This, BTW, should be almost always included if you're using the identity transform template.

like image 181
michael.hor257k Avatar answered Oct 18 '25 10:10

michael.hor257k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!