Xslt XSL。根据数据文本中指定的格式显示文本
对不起我的英语 我编写了XML示例:Xslt XSL。根据数据文本中指定的格式显示文本,xslt,Xslt,对不起我的英语 我编写了XML示例: <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="./test.xslt"?> <document> <paragraph id="p1"> I like <i>the flowers</i>!!! </paragraph>
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="./test.xslt"?>
<document>
<paragraph id="p1">
I like <i>the flowers</i>!!!
</paragraph>
<paragraph id="p2">
<![CDATA[I like <i>the people</i>!!!]]>
</paragraph>
</document>
我喜欢这些花/我!!!
人民!!!]>
我为它编写了XSL示例:
<?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"/>
<xsl:template match="/">
<html>
<body>
<p>
<xsl:value-of select="/document/paragraph[@id='p1']"/>
<br/>
<xsl:value-of select="/document/paragraph[@id='p2']"/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我在文本值(某些文本)中指定了格式。但格式化不会发生。我在浏览器中获得下一个结果:
I like <i>the flowers</i>!!!
I like <i>the people</i>!!!
我喜欢这些花!!!
我喜欢的人!!!
如何强制应用指定的格式
这是一个常见问题 被破坏的标记(如序列化为转义字符串表示)由浏览器显示为文本(这正是转义字符的解释方式) 为了获得所需的格式,请不要破坏标记 而不是:
I like <i>the flowers</i>!!!
使用:
我喜欢这些花!!!
此外,替换:
<xsl:value-of select="/document/paragraph[@id='p1']"/>
与:
总结如下:
使用此XML文档:
<document>
<paragraph id="p1">
I like <i>the flowers</i>!!!
</paragraph>
<paragraph id="p2">I like <i>the people</i>!!!</paragraph>
</document>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<p>
<xsl:copy-of select="/document/paragraph[@id='p1']/node()"/>
<br/>
<xsl:copy-of select="/document/paragraph[@id='p2']/node()"/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<html>
<body>
<p>
I like <i>the flowers</i>!!!
<br>I like <i>the people</i>!!!
</p>
</body>
</html>
我喜欢这些花!!!
我喜欢的人!!!
并将转换更改为此:
<document>
<paragraph id="p1">
I like <i>the flowers</i>!!!
</paragraph>
<paragraph id="p2">I like <i>the people</i>!!!</paragraph>
</document>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<p>
<xsl:copy-of select="/document/paragraph[@id='p1']/node()"/>
<br/>
<xsl:copy-of select="/document/paragraph[@id='p2']/node()"/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<html>
<body>
<p>
I like <i>the flowers</i>!!!
<br>I like <i>the people</i>!!!
</p>
</body>
</html>
这将产生所需的正确结果:
<document>
<paragraph id="p1">
I like <i>the flowers</i>!!!
</paragraph>
<paragraph id="p2">I like <i>the people</i>!!!</paragraph>
</document>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<p>
<xsl:copy-of select="/document/paragraph[@id='p1']/node()"/>
<br/>
<xsl:copy-of select="/document/paragraph[@id='p2']/node()"/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<html>
<body>
<p>
I like <i>the flowers</i>!!!
<br>I like <i>the people</i>!!!
</p>
</body>
</html>
我喜欢这些花!!!
我喜欢的人!!!
并在浏览器中显示如下内容:
<document>
<paragraph id="p1">
I like <i>the flowers</i>!!!
</paragraph>
<paragraph id="p2">I like <i>the people</i>!!!</paragraph>
</document>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<p>
<xsl:copy-of select="/document/paragraph[@id='p1']/node()"/>
<br/>
<xsl:copy-of select="/document/paragraph[@id='p2']/node()"/>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<html>
<body>
<p>
I like <i>the flowers</i>!!!
<br>I like <i>the people</i>!!!
</p>
</body>
</html>
我喜欢这些花!!!
我喜欢的人!!!
我很难理解上面评论中的讨论,因为我对英语理解得很差,对XSL的理解仍然很弱。对我来说,使用“disable-output-escaping=”yes“的选项似乎简单方便。“xsl:copy of”选项对我来说也很有趣,我对此表示感谢。不清楚。。。到底是什么事情没有发生,你期望的,你是如何指定你的格式的?哦,我找到了答案<代码>代码
参数“disable output escaping=“yes”解决了此问题problem@Bush确切地你应该把它写下来作为对你自己问题的回答,而不是评论。我不能。我收到消息:“哎呀!您的答案无法提交,因为:声誉低于100的用户在提问后8小时内无法回答自己的问题。您可以在5小时内自行回答。在此之前,请使用评论或编辑您的问题。”虽然这是真的,只要
的内容是格式良好的XML,它就可以工作。但它可能是HTML的一个片段,需要CDATA或XML编码,这反过来会使禁用输出转义成为必要。@Tomalak:DOE不在XSLT 2.0及以上版本中。即使在XSLT1.0中,DOE也不是强制性的特性,一些XSLT1.0处理器也没有实现它。DOE反对XSLT的体系结构模型,因此最好避免。如果有人想用XSLT处理一个不是格式良好的XML的HTML实例,他们总是可以使用一个类型化的预处理步骤(相当多的现有系统可以方便地这样做),或者使用一个扩展解析函数,将格式不好的HTML片段转换成格式良好的XML片段。没错。DOE并不干净,但是当XSLT处理器提供该特性时,为什么不使用它呢?对于给定的问题,这是一个快速成功的解决方案。插入一个将HTML内容净化为XML兼容的步骤是可能的,但也有其缺点。例如,HTML5并不完全是XML。它可能包含XML未知的命名实体引用。当然有办法解决这个问题,但务实的方法并不是最坏的选择。@Tomalak:今天的开发人员编写客户端XSLT转换,并希望无论使用何种浏览器都能成功地处理这些转换。不可能,如果他们使用DOE的话。我注意到了这种趋势。我从来都不太关心客户端转换。客户端XSLT支持的级别与可能的目标平台的类型一样不可预测和不同。我倾向于默认使用服务器端,我仍然认为这是更合理的方法。布什,关于DOE的讨论正好讨论了为什么不使用DOE(禁用输出转义)是明智的。