XSLT解析存储在属性中的转义HTML,并将该属性的内容转换为元素的内容 [英] XSLT parse escaped HTML stored in an attribute and convert that attribute's content into element's content
问题描述
我坚持我认为应该做的简单的事情.我一直在环顾四周,但是没有找到解决方案.希望你能帮助我.
I'm stuck on what I think should be simple thing to do. I've been looking around, but didn't find the solution. Hope you will help me.
我所拥有的是一个XML元素,其属性包含转义的HTML元素:
What I have is an XML element with an attribute that contains escaped HTML elements:
<Booking>
<BookingComments Type="RAM" comment="RAM name fred<br/>Tel 09876554<br/>Email fred@bla.com" />
</Booking>
我需要获取的是将@comment属性中的HTML元素和内容解析为
元素的内容,如下所示:
What I need to get is parsed HTML elements and content from the @comment attribute to be a content of
element as follows:
<p>
RAM name fred<br/>Tel 09876554<br/>Email fred@bla.com
<p>
这是我的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>
我已经阅读到copy-of是将转义的HTML元素还原回适当元素的好方法.在这种特定情况下,因为它最初是属性,所以copy-of也会将其转换为属性.所以我得到:
I've read that copy-of is a good way to restore escaped HTML elements back to proper elements. In this specific case, because it's initially an attribute the copy-of translates it into attribute as well. So I get:
<p comment="RAM name fred<br/>Tel 09876554<br/>Email fred@bla.com"></p>
那不是我想要的.
如果我使用apply-templates而不是copy-of,例如:
If I use apply-templates instead of copy-of, as in:
<p>
<xsl:apply-templates select="/Booking/BookingComments[lower-case(@Type)='ram'[1]/@comment"/>
</p>
我只是以文本形式获取p的内容,而不是还原的HTML元素.
I get p's content simply as text, not restored HTML elements.
<p>RAM name fred<br/>Tel 09876554<br/>Email fred@bla.com</p>
我确定我错过了一些过时的东西.我真的很感谢您的帮助和提示!
I'm sure I'm missing something obvous. I would really appreciate any help and tips!
推荐答案
我建议使用专用模板:
<!-- 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>
这样,您可以简单地将模板应用于属性.请注意,禁用输出转义可能会生成格式错误的输出.
This way you could simply apply templates to the attribute. Note that disabling output escaping has the potential to generate ill-formed output.
这篇关于XSLT解析存储在属性中的转义HTML,并将该属性的内容转换为元素的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!