gpt4 book ai didi

html - 如何处理 XSL 中的非法 HTML 字符

转载 作者:太空狗 更新时间:2023-10-29 14:55:40 24 4
gpt4 key购买 nike

我有一个存储数据的 XML 文件。我正在使用 XSL 从该 XML 文件生成 HTML 文件。当我尝试这样做时,出现错误 Illegal HTML character: decimal 150

我不允许更改 XML 文件。我必须将一个和许多其他非法字符映射到 XSL 中的合法字符(可以是任何字符)。因此,它必须以一种通用的方式进行映射,而不仅仅是针对一种类型的字符。

最佳答案

您可以定义一个字符映射表,将不允许的字符映射到允许的字符,例如空格:

<xsl:output indent="yes" method="html" use-character-maps="m1"/>

<xsl:character-map name="m1">
<xsl:output-character character="&#150;" string=" "/>
</xsl:character-map>

作为替代方案,根据 http://www.w3.org/TR/xslt-xquery-serialization/#HTML_CHARDATA 使用模板替换所有非法字符这些是控制字符 #x7F-#x9F 所以使用

<xsl:template match="text()">
<xsl:value-of select="replace(., '[&#x007F;-&#x009F;]', ' ')"/>
</xsl:template>

应确保输入文档中文本节点中的那些字符被空格替换。

作为另一种选择,您可以考虑使用 XHTML namespace 中的元素和输出方法 xhtml 来输出 XHTML。

基于字符列表,将所有非法控制字符映射到一个空格的全字符映射是

<xsl:character-map
name="no-control-characters">
<xsl:output-character character="&#127;" string=" "/>
<xsl:output-character character="&#128;" string=" "/>
<xsl:output-character character="&#129;" string=" "/>
<xsl:output-character character="&#130;" string=" "/>
<xsl:output-character character="&#131;" string=" "/>
<xsl:output-character character="&#132;" string=" "/>
<xsl:output-character character="&#133;" string=" "/>
<xsl:output-character character="&#134;" string=" "/>
<xsl:output-character character="&#135;" string=" "/>
<xsl:output-character character="&#136;" string=" "/>
<xsl:output-character character="&#137;" string=" "/>
<xsl:output-character character="&#138;" string=" "/>
<xsl:output-character character="&#139;" string=" "/>
<xsl:output-character character="&#140;" string=" "/>
<xsl:output-character character="&#141;" string=" "/>
<xsl:output-character character="&#142;" string=" "/>
<xsl:output-character character="&#143;" string=" "/>
<xsl:output-character character="&#144;" string=" "/>
<xsl:output-character character="&#145;" string=" "/>
<xsl:output-character character="&#146;" string=" "/>
<xsl:output-character character="&#147;" string=" "/>
<xsl:output-character character="&#148;" string=" "/>
<xsl:output-character character="&#149;" string=" "/>
<xsl:output-character character="&#150;" string=" "/>
<xsl:output-character character="&#151;" string=" "/>
<xsl:output-character character="&#152;" string=" "/>
<xsl:output-character character="&#153;" string=" "/>
<xsl:output-character character="&#154;" string=" "/>
<xsl:output-character character="&#155;" string=" "/>
<xsl:output-character character="&#156;" string=" "/>
<xsl:output-character character="&#157;" string=" "/>
<xsl:output-character character="&#158;" string=" "/>
<xsl:output-character character="&#159;" string=" "/>
</xsl:character-map>

我使用 XSLT 2.0 和 Saxon 生成了该列表,使用

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:axsl="http://www.w3.org/1999/XSL/TransformAlias"
exclude-result-prefixes="xs axsl">

<xsl:param name="start" as="xs:integer" select="127"/>
<xsl:param name="end" as="xs:integer" select="159"/>

<xsl:param name="replacement" as="xs:string" select="' '"/>

<xsl:namespace-alias stylesheet-prefix="axsl" result-prefix="xsl"/>

<xsl:output method="xml" indent="yes" use-character-maps="character-reference"/>

<xsl:character-map name="character-reference">
<xsl:output-character character="«" string="&amp;"/>
</xsl:character-map>

<xsl:template name="main">
<axsl:character-map name="no-control-characters">
<xsl:for-each select="$start to $end">
<axsl:output-character character="«#{.};" string="{$replacement}"/>
</xsl:for-each>
</axsl:character-map>
</xsl:template>

</xsl:stylesheet>

关于html - 如何处理 XSL 中的非法 HTML 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23156550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com