Export (0) Print
Expand All

<xsl:text> Element

Generates text node from a style sheet. White-space-only nodes are preserved in the output.

<xsl:text
  disable-output-escaping = "yes" | "no">
</xsl:text>

disable-output-escaping
Default is "no". If the value is "yes", a text node generated by instantiating the <xsl:text> element will be output without any escaping. For example, the following generates the single character "<".

<xsl:text disable-output-escaping="yes">&lt;</xsl:text>

noteNote:
Because disable-output-escaping="yes" can be used to generate non-well-formed documents, it should be used with caution. Output that is not well-formed can generate errors in certain circumstances. For example, transformNodeToObject to an XML document requires that the result be well-formed and thus might not complete if disable-output-escaping has affected the well-formedness of the document. Consider disable-output-escaping="yes" an advanced feature, to be used only when the potential dangers are understood.

 

Number of occurrences

Unlimited

Parent elements

xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:fallback, xsl:for-each, xsl:if, xsl:otherwise, xsl:message, xsl:param, xsl:processing-instruction, xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements

Child elements

(No child elements)

In a style sheet, text can be generated to the literal result tree with or without <xsl:text>. However, with this element you can exert some control over the white space created by the style sheet. For example, to make your style sheet more readable, you might want to write one element per line in a template, and indent some lines. Doing so introduces white space as part of the template rule. This might or might not be a desired effect of the transformation.

Sometimes you might want to introduce a white space character to separate two data values. You can use an <xsl:text> element to accomplish this. White space enclosed within <xsl:text> is output to the result tree. Thus, the following template—

<xsl:template match="a-node">
   <xsl:text>
   </xsl:text>
</xsl:template>

—will always output a new line text node in the result tree. However, white-space-only text nodes not enclosed by <xsl:text> will be stripped away from the result tree. The example below how to use empty <xsl:text/> elements to this.

XML File (text.xml)

<?xml version="1.0"?>
<topic>
  <text>First line.</text>
  <text>Second line.</text>
  <text></text>
</topic>

XSLT File (text.xsl)

The following style sheet employs empty <xsl:text/> elements to strip all white space characters (spaces, newlines, and tabs) that would otherwise have been generated by the template rules. The result is shown in the formatted output, below.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="text">
    <xsl:text/>"<xsl:value-of select="."/>"<xsl:text/>
  </xsl:template>
</xsl:stylesheet>

HTML File (text.htm)

The following HTML file can be used to try the XSLT transformation and see the results.

<html>
  <head>
    <title></title>
  </head>
  <body onload="init()">
     <div><input type="text" id="xmlName" value="text.xml"></div>
     <div><input type="text" id="xslName" value="text.xsl"></div>
     <div><input type=button value="transform" onclick="trans();"></div>
     <div id="divErr"></div>
     <pre id="preRes" style="background:blue;color:gold"></pre>
  </body>

  <script language="javascript">
    function trans() 
    {
      xmlFile=xmlName.value;
      xslFile=xslName.value;
      if (xmlFile == "" || xslFile == "") 
      {
         divErr.innerHTML = "invalid xml/xsl file names.";
      }

      var xsl = new ActiveXObject("MSXML2.DOMDOCUMENT.6.0");
      var xml = new ActiveXObject("MSXML2.DOMDocument.6.0");
      xml.validateOnParse = false;
      xml.async = false;
      xml.load(xmlFile);
      if (xml.parseError.errorCode != 0)
        divErr.innerHTML = "XML Parse Error : " + xml.parseError.reason;

      xsl.async = false;
      xsl.load(xslFile);
      if (xsl.parseError.errorCode != 0)
        divErr.innerHTML = "XSL Parse Error : " + xsl.parseError.reason;

      try
      {
        res = xml.transformNode(xsl.documentElement);
        preRes.innerText = res;
      }
      catch(err)
      {
        divErr.innerHTML = "Transformation Error:"
               +err.number+"*"+err.description;
      }
    }
  </script>
</html>

Try It!

  1. Copy the sample code into appropriate files, and save the files to your local drive.

  2. Double-click your HTML file, text.htm.

  3. Click the transform button on the Web page that appears.

Output

When the XSLT style sheet above is used as is, all text values are joined, head-to-end, in a single line of output:

"First line.""Second line."""

If you remove both <xsl:text/> elements from the template matching text elements, you get the following output (in 7 lines):

"First line."

"Second line."

""

If you remove only the first <xsl:text/> element from the template, you get the following output (in 4 lines):

"First line."

"Second line."

""




Build Date:

2012-08-02
Show:
© 2014 Microsoft