XSLT parsing accelerates the HTML stored in the attribute and converts this attribute content to element content

I'm stuck on what I think should be simple. I searched around but couldn't find a solution. I hope you help me.

I have an XML element with an attribute that contains escaped HTML elements:

<Booking>    
  <BookingComments Type="RAM" comment="RAM name fred&lt;br/&gt;Tel 09876554&lt;br/&gt;Email fred@bla.com" />
</Booking>

What I need to get is to parse the HTML elements and content from the @comment attribute to be the content of the element

in the following way:

<p>
  RAM name fred<br/>Tel 09876554<br/>Email fred@bla.com
<p>

Here is my XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="xs fn" version="1.0">


<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" 
    doctype-system="http://www.w3.org/TR/html4/loose.dtd" encoding="UTF-8" indent="yes" />

 <xsl:template name="some-template">
   <p>Some text</p>
   <p>
      <xsl:copy-of
        select="/Booking/BookingComments[lower-case(@Type)='ram'][1]/@comment"/>
   </p>     
 </xsl:template>
</xsl:stylesheet>

I read that copy is a good way to restore escaped HTML elements back to their respective elements. In this particular case, since the attribute is first copied, it will also convert it to an attribute. Therefore, I get:

<p comment="RAM name fred<br/&gt;Tel 09876554<br/&gt;Email fred@bla.com"></p>

This is not what I want.

If I use apply patterns instead of patterns, as in:   

       

p- , HTML-.

<p>RAM name fred&lt;br/&gt;Tel 09876554&lt;br/&gt;Email fred@bla.com</p>

, - . !

.

+3
2

:

<!-- check if lower-casing @Type is really necessary -->
<xsl:template name="BookingComments[lower-case(@Type)='ram']/@comment">
  <p>
    <xsl:value-of select="." disable-output-escaping="yes" />
  </p>     
</xsl:template>

, . , .

+4

parse() , . XSLT.

Xalan :

public class MyExtension
{
    public static NodeIterator Parse( string xml );
}

:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:java="http://xml.apache.org/xalan/java"
    exclude-result-prefixes="java"
    version="1.0">

    <xsl:template match="BookingComments">
        <xsl:copy-of select="java:package.name.MyExtension.Parse(string(@comment))" />
    </xsl:template>

</xsl:stylesheet>
0

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


All Articles