how to remove line break in xslt

2019-09-05 16:02发布

I am using the xsl:copy-of to display the complete node but it adds additional lines to the top and bottom.

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<root>
  <E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <ApplicationData>
      <TraceData>
        <DataItem>
          <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information">
            <TraceIdentifier>MessageSent.aspx</TraceIdentifier>
          </TraceRecord>
        </DataItem>
        <DataItem>
          <table>
            <tr>
              <td>This should not be a table</td>
              <td>It must be a text</td>
            </tr>
          </table>
        </DataItem>
      </TraceData>
    </ApplicationData>
  </E2ETraceEvent>
</root>

XSLT

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:te="http://schemas.microsoft.com/2004/06/E2ETraceEvent"
                exclude-result-prefixes="te">
  <xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <output>
      <html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <body>
          <table>
            <thead>
              <tr>
                <td>Data</td>
              </tr>
            </thead>
            <tbody>
              <xsl:for-each select="//te:E2ETraceEvent">
              <tr>
                <td>
                  <table>
                    <xsl:for-each select=".//te:TraceData//te:DataItem">
                      <tr>
                        <td>
                          <xmp>
                            <xsl:copy-of select="./node()" />
                          </xmp>
                        </td>
                      </tr>
                    </xsl:for-each>
                  </table>
                </td>
              </tr>
              </xsl:for-each>
            </tbody>
          </table>
        </body>
      </html>
    </output>
  </xsl:template>

</xsl:stylesheet>

Output

between quotes there is a space and empty lines above and below the actual node

"
        <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information">
            <TraceIdentifier>MessageSent.aspx</TraceIdentifier>
        </TraceRecord>
    "

Desired Output

between quotes there is a space but no line, inside of the node

"       <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Information">
            <TraceIdentifier>MessageSent.aspx</TraceIdentifier>
        </TraceRecord>"

标签: xml xslt
1条回答
霸刀☆藐视天下
2楼-- · 2019-09-05 16:31

Changing the part ./node() to * should solve the issue

Also you may probably ask how to remove the namespace that adds into the table tag

Based on Remove namespace declaration from XSLT stylesheet with XSLT add following parts

<!-- Copy elements -->
<xsl:template match="*" priority="-1">
   <xsl:element name="{name()}">
      <xsl:apply-templates select="node()|@*"/>
   </xsl:element>
</xsl:template>

<!-- Copy all other nodes -->
<xsl:template match="node()|@*" priority="-2">
   <xsl:copy />      
</xsl:template>

and change

<xsl:copy-of select="*"/>

to

<xsl:apply-templates select="*"/>
查看更多
登录 后发表回答