Why the extra text in my XSLT output?

Related searches

Below is an example of the XML and XSLT:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <count>5</count>
  <idx>10</idx>
  <cds>
    <cd>
      <title>Empire Burlesque</title>
      <artist>Bob Dylan</artist>
      <country>USA</country>
      <company>Columbia</company>
      <price>10.90</price>
      <year>1985</year>
    </cd>
    <cd>
      <title>Hide your heart</title>
      <artist>Bonnie Tyler</artist>
      <country>UK</country>
      <company>CBS Records</company>
      <price>9.90</price>
      <year>1988</year>
    </cd>
  </cds>
</catalog>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" omit-xml-declaration="yes" standalone="yes" />
    <xsl:template match="/catalog/cds">
      <html>
      <body>
      <h2>My CD Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th style="text-align:left">Title</th>
            <th style="text-align:left">Artist</th>
          </tr>
          <xsl:for-each select="cd">
          <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="artist"/></td>
          </tr>
          </xsl:for-each>
        </table>
      </body>
      </html>
    </xsl:template>
</xsl:stylesheet>

Question: Why is it that when transforming the given XML document the content of the elements count and idx is being printed out before the table all the time?


The XSLT built-in templates (documentation for XSLT2 and XSLT3) are doing that. Specifically:

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

You can override, or simply change your template from matching cds

<xsl:template match="/catalog/cds">

to matching /:

<xsl:template match="/">

and the spurious text node output will be avoided.

Why the extra text in my XSLT output? - xml - html, XSLT � Elements � xsl:text Syntax: The xsl:text element is used to add literal text to the output. Normally, any text that occurs in a stylesheet will be copied to the output In other words, a text will be displayed as it really is without any extra� Hi The following is the output from xslt processing (see below). Context vs2010 / .net 4 XSLT processing. => Why have <br /> br been replaced with the ridiculous unnecessary


The only template that you have written matches <xsl:template match="/catalog/cds">, processing however starts at the document node, in your case with a built-in template https://www.w3.org/TR/xslt20/#built-in-rule, so you will have to write a template matching / or /catalog or you will have to make sure you write templates that don't output anything for the elements you don't want to produce any output.

XSLT >> Elements >> xsl:text, Note: <xsl:output> is a top-level element, and must appear as a child node of <xsl :stylesheet> or <xsl:transform>. Syntax. <xsl:output method="xml|html|text|name" Definition and Usage. The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.


Another option is to override the built-in template rule in your stylesheet with a version that does nothing:

<xsl:template match="text()|@*" mode="#all"/>

XSLT <xsl:output>, The <xsl:text> element is used to write literal text to the output. Tip: This xmlns: xsl="http://www.w3.org/1999/XSL/Transform"> <h2>My CD Collection</h2> Look for the hex or decimal entities representing carraige return and/or line feed. If you put them in you xslt file, they will be interpreted as ascii characters in your text reader (ie. notepad). For example, space = (the one I remember off the top of my head).


XSLT <xsl:text>, <xsl:output method="text" encoding="UTF-8"/> I don't think there is a way to overcome this without wrapping your output text in an XML tag,� The result of applying XSLT to XML document could be an another XML document, HTML, text or any another document from technology prespective. The XSL code is written within the XML document with the extension of (.xsl). In other words, an XSLT document is a different kind of XML document. XML Namespace: XML Namespaces are the unique names .


XSLT which output non-xml text in XProc, The <xsl:value-of> element is used to extract the value of a selected node. The <xsl:value-of> Element The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation:


The <xsl:copy-of> element creates a copy of the current node. Note: Namespace nodes, child nodes, and attributes of the current node are automatically copied as well! Tip: This element can be used to insert multiple copies of the same node into different places in the output.