XSLT Special Characters

In the following XSL transformation, how can I output '<' and '>'?

XML input:

<TestResult bugnumber="3214" testname="display.methods::close->test_ManyInvoke" errortype="Failure"><ErrorMessage><![CDATA[calling close() method failed - expected:<2>]]></ErrorMessage>

XSLT:

<xsl:template match="TestResult">
  <xsl:variable name="errorMessage">
   <xsl:value-of select="ErrorMessage" disable-output-escaping="yes"/>
  </xsl:variable>
  <Test name='{@testname}'>
   <TestResult>
    <Passed>false</Passed>
    <State>failure</State>
    <Metadata>
     <Entry name='bugnumber' value='{@bugnumber}' />
    </Metadata>
    <TestOutput>
     <Metadata>
      <Entry name='ErrorMessage' value='{$errorMessage}' />
     </Metadata>
    </TestOutput>
   </TestResult>
  </Test>
 </xsl:template>

XML Output:

<Test name="display.methods::close-&gttest_ManyInvoke"> 
 <TestResult>
  <Passed>false</Passed>
  <State>failure</State>
  <Metadata>
   <Entry name="bugnumber" value="3214"/>
  </Metadata>
  <TestOutput>
   <Metadata>
    <Entry name="ErrorMessage" value="calling close() method failed - expected:&lt;2&gt;"/>
   </Metadata>
  </TestOutput>
 </TestResult>
</Test>
+3
source share
4 answers

The short answer . You can not.

Long answer . An attribute value cannot contain several special characters, such as '<', '>'and '&'.

If they are present, they run as '&lt;', '&gt;'and '&amp;'.

These characters can be created if the output method is “text,” which is not your case.

, , : : '&lt;', '&gt;' '&amp;'.

, node, CDATA. :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
 <xsl:output cdata-section-elements="t"
  omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <t> &lt; &amp; &gt;</t>
 </xsl:template>
</xsl:stylesheet>

XML- ( ), :

<t><![CDATA[ < & >]]></t>
+7

, &gt; , , , XML, , .

XML unescape , . XML- XML-, ; , display.methods::close->test_ManyInvoke.

, "" :

  • -, , XML, : ", ".

  • -, , XML, XML. ( " - XML, ?" , , .)

, - disable-output-escaping='yes' XSLT CDATA, , , . - - , , , " CDATA", .

+1

, - . ,

test="$some_variable = 'Wendy&apos;s'"

. escape-,

test="$some_variable = 'Wendy&amp;apos;s'
0

, , , , , , , < > , .

<xsl:value-of disable-output-escaping="yes" select="string('&lt;')"/>

- MSDN.

0

Source: https://habr.com/ru/post/1747457/


All Articles