Html 如何在xslt中为CDATA的特殊元素设置异常
事情是这样的。我有一个可以读取XML和html的查看器。现在我想实现以下目标。我希望将Html 如何在xslt中为CDATA的特殊元素设置异常,html,xml,xslt,cdata,Html,Xml,Xslt,Cdata,事情是这样的。我有一个可以读取XML和html的查看器。现在我想实现以下目标。我希望将中的文本转换为CDATA(选中),但如果在中使用了特定元素,*我们将该元素称为,则该元素中的所有文本都不应转换为CDATA 这可能吗?我已经尝试了不同的事情,但还没有找到一个解决方案来帮助我解决我的问题 输入数据如下: <infobox> <title>HTML TEST</title> <note><b>XML in the Note<
中的文本转换为CDATA(选中),但如果在
中使用了特定元素,*我们将该元素称为
,则该元素中的所有文本都不应转换为CDATA
这可能吗?我已经尝试了不同的事情,但还没有找到一个解决方案来帮助我解决我的问题
输入数据如下:
<infobox>
<title>HTML TEST</title>
<note><b>XML in the Note</b><html html="yes"><h1>Headline</h1>
<h2 style="color:green"> Grüne Schrift </h2>
<h3 style="color:black"> Schwarze Schrift </h3>
<h4 style="color:#FF0000"> Rote Schrift </h4></html>
</note>
</infobox>
<xsl:template match="note[not(ancestor::info) and not(ancestor::item-content) and not(ancestor::context)]">
<note type="{@type}">
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<div class="{$noteClass}">
<xsl:apply-templates/>
</div>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</note>
</xsl:template>
<infobox>
<title id="CAP0e6ba25c-a8a1-45fb-a957-43c3195f9c326">HTML TEST</title>
<note type=""><![CDATA[<div class="NoteContent">
<b>XML in the Note</b>
<html><<div class="html-content"><h1>Headline</h1><h2 style="color:green"> Grüne Schrift </h2>
<h3 style="color:black"> Schwarze Schrift </h3>
<h4 style="color:#FF0000"> Rote Schrift </h4></div>]]></html>
</div>]]></note>
</infobox>
<infobox>
<title id="CAP0e6ba25c-a8a1-45fb-a957-43c3195f9c326">HTML TEST</title>
<note type=""><![CDATA[<div class="NoteContent">
<b>XML in the Note</b></div>]]><html><h1>Headline</h1><h2 style="color:green"> Grüne Schrift </h2> <h3 style="color:black"> Schwarze Schrift </h3> <h4 style="color:#FF0000"> Rote Schrift </h4></html>
</note>
</infobox>
HTML测试
Noteh1Headline/h1中的XML
h2 style=“color:green”Grüne Schrift/h2
h3 style=“颜色:黑色”Schwarze Schrift/h3
h4 style=“color:#FF0000”Rote Schrift/h4
我的注释XSL部分如下:
<infobox>
<title>HTML TEST</title>
<note><b>XML in the Note</b><html html="yes"><h1>Headline</h1>
<h2 style="color:green"> Grüne Schrift </h2>
<h3 style="color:black"> Schwarze Schrift </h3>
<h4 style="color:#FF0000"> Rote Schrift </h4></html>
</note>
</infobox>
<xsl:template match="note[not(ancestor::info) and not(ancestor::item-content) and not(ancestor::context)]">
<note type="{@type}">
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<div class="{$noteClass}">
<xsl:apply-templates/>
</div>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</note>
</xsl:template>
<infobox>
<title id="CAP0e6ba25c-a8a1-45fb-a957-43c3195f9c326">HTML TEST</title>
<note type=""><![CDATA[<div class="NoteContent">
<b>XML in the Note</b>
<html><<div class="html-content"><h1>Headline</h1><h2 style="color:green"> Grüne Schrift </h2>
<h3 style="color:black"> Schwarze Schrift </h3>
<h4 style="color:#FF0000"> Rote Schrift </h4></div>]]></html>
</div>]]></note>
</infobox>
<infobox>
<title id="CAP0e6ba25c-a8a1-45fb-a957-43c3195f9c326">HTML TEST</title>
<note type=""><![CDATA[<div class="NoteContent">
<b>XML in the Note</b></div>]]><html><h1>Headline</h1><h2 style="color:green"> Grüne Schrift </h2> <h3 style="color:black"> Schwarze Schrift </h3> <h4 style="color:#FF0000"> Rote Schrift </h4></html>
</note>
</infobox>
![CDATA[
]]
我发布的结果如下:
<infobox>
<title>HTML TEST</title>
<note><b>XML in the Note</b><html html="yes"><h1>Headline</h1>
<h2 style="color:green"> Grüne Schrift </h2>
<h3 style="color:black"> Schwarze Schrift </h3>
<h4 style="color:#FF0000"> Rote Schrift </h4></html>
</note>
</infobox>
<xsl:template match="note[not(ancestor::info) and not(ancestor::item-content) and not(ancestor::context)]">
<note type="{@type}">
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<div class="{$noteClass}">
<xsl:apply-templates/>
</div>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</note>
</xsl:template>
<infobox>
<title id="CAP0e6ba25c-a8a1-45fb-a957-43c3195f9c326">HTML TEST</title>
<note type=""><![CDATA[<div class="NoteContent">
<b>XML in the Note</b>
<html><<div class="html-content"><h1>Headline</h1><h2 style="color:green"> Grüne Schrift </h2>
<h3 style="color:black"> Schwarze Schrift </h3>
<h4 style="color:#FF0000"> Rote Schrift </h4></div>]]></html>
</div>]]></note>
</infobox>
<infobox>
<title id="CAP0e6ba25c-a8a1-45fb-a957-43c3195f9c326">HTML TEST</title>
<note type=""><![CDATA[<div class="NoteContent">
<b>XML in the Note</b></div>]]><html><h1>Headline</h1><h2 style="color:green"> Grüne Schrift </h2> <h3 style="color:black"> Schwarze Schrift </h3> <h4 style="color:#FF0000"> Rote Schrift </h4></html>
</note>
</infobox>
HTML测试
注释中的XML
h1Headline/h1h2 style=“color:green”Grüne Schrift/h2
h3 style=“颜色:黑色”Schwarze Schrift/h3
h4 style=“color:#FF0000”Rote Schrift/h4]]
]]>
我想要公布的结果是:
<infobox>
<title>HTML TEST</title>
<note><b>XML in the Note</b><html html="yes"><h1>Headline</h1>
<h2 style="color:green"> Grüne Schrift </h2>
<h3 style="color:black"> Schwarze Schrift </h3>
<h4 style="color:#FF0000"> Rote Schrift </h4></html>
</note>
</infobox>
<xsl:template match="note[not(ancestor::info) and not(ancestor::item-content) and not(ancestor::context)]">
<note type="{@type}">
<xsl:text disable-output-escaping="yes"><![CDATA[</xsl:text>
<div class="{$noteClass}">
<xsl:apply-templates/>
</div>
<xsl:text disable-output-escaping="yes">]]></xsl:text>
</note>
</xsl:template>
<infobox>
<title id="CAP0e6ba25c-a8a1-45fb-a957-43c3195f9c326">HTML TEST</title>
<note type=""><![CDATA[<div class="NoteContent">
<b>XML in the Note</b>
<html><<div class="html-content"><h1>Headline</h1><h2 style="color:green"> Grüne Schrift </h2>
<h3 style="color:black"> Schwarze Schrift </h3>
<h4 style="color:#FF0000"> Rote Schrift </h4></div>]]></html>
</div>]]></note>
</infobox>
<infobox>
<title id="CAP0e6ba25c-a8a1-45fb-a957-43c3195f9c326">HTML TEST</title>
<note type=""><![CDATA[<div class="NoteContent">
<b>XML in the Note</b></div>]]><html><h1>Headline</h1><h2 style="color:green"> Grüne Schrift </h2> <h3 style="color:black"> Schwarze Schrift </h3> <h4 style="color:#FF0000"> Rote Schrift </h4></html>
</note>
</infobox>
HTML测试
注释中的XML]]>h1Headline/h1h2 style=“color:green”Grüne Schrift/h2 h3 style=“color:black”Schwarze Schrift/h3 h4 style=“color:#FF0000”Rote Schrift/h4
因此您可以看到,
元素应该被排除在转换之外
我愿意听取任何关于编辑显示的xsl部分或任何其他xsl输入的建议
谢谢大家。在XSLT 3中,选择非
html
元素并将它们包装在序列化的div中是很容易做到的:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="note/*[not(self::html)]">
<xsl:variable name="div-wrapper" as="element(div)">
<div class="note">
<xsl:apply-templates/>
</div>
</xsl:variable>
<xsl:value-of select="serialize($div-wrapper)"/>
</xsl:template>
</xsl:stylesheet>
创建CDATA节可以委托给
,请参见,但正如您在那里看到的那样,很难控制注释
中仅包含空格的内容会发生什么情况,在您的示例中,您将得到一个在html
元素之后带有空格的CDATA节。是否需要CDATA节?还是仅仅是对内容的逃避?您使用哪个XSLT处理器,哪个XSLT版本?我确实需要CDATA部分。只有html元素中的内容应按原样转发。一定是我的。2.0您使用哪个版本的XSLT 2处理器?它是否具有序列化节点的扩展功能?亲爱的Martin Honnen,非常感谢。你的回答帮了我很大的忙,效果非常好。我的成绩如所愿。html
周围的所有内容都将发布到CDATA,而html
it本身将以1:1的比例转发。