First
Se" />
First
Se" />
First
Se"/>

I would like to remove all of the carriage returns and line feeds between the elements. I'm using Microsoft Visual Studio. So, this is a 1.0 issue

37 views Asked by At

Input XML:

<?xml version="1.0" encoding="utf-8"?>
<section>
  <table>
    <tbody>
      <tr>
        <td>
          <datapoint>First</datapoint>
        </td>
      </tr>
      <tr>
        <td>
          <datapoint>Second</datapoint>
        </td>
      </tr>
    </tbody>
  </table>
</section>

This is what I tried:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  exclude-result-prefixes="msxml xsl">

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

  <xsl:output indent="yes" method="xml" version="1.0" omit-xml-declaration="no"/>

  <xsl:template match="text()[contains(.,'&#10;') or contains(.,'&#13;')]"/>

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


</xsl:stylesheet>

This is what I was expecting:

Expected result: (one line)

<?xml version="1.0" encoding="utf-8"?><section><table><tbody><tr><td><datapoint>First</datapoint></td></tr><tr><td><datapoint>Second</datapoint></td></tr></tbody></table></section>

Actual Result: (no change)

<?xml version="1.0" encoding="utf-8"?>
<section>
  <table>
    <tbody>
      <tr>
        <td>
          <datapoint>First</datapoint>
        </td>
      </tr>
      <tr>
        <td>
          <datapoint>Second</datapoint>
        </td>
      </tr>
    </tbody>
  </table>
</section>

I'm wondering if I'm removing the linefeeds and carriage returns, but VS is putting them right back in.

1

There are 1 answers

1
michael.hor257k On BEST ANSWER

How about:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>

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

</xsl:stylesheet>