gpt4 book ai didi

xslt - 如何使用 XSLT 用它们的转义变体替换某些字符?

转载 作者:行者123 更新时间:2023-12-01 01:33:20 25 4
gpt4 key购买 nike

我正在尝试开发一个 XSLT 样式表来转换给定的 DocBook将文档转换为可以提供给 lout 的文件文档格式系统(然后生成 PostScript 输出)。

这样做需要我替换 DocBook 元素文本中的几个字符,因为它们具有特殊的含义。尤其是人物

/ | & { } # @ ~ \ "

需要用双引号 (") 括起来,以便 lout 将它们视为普通字符。

例如,像这样的 DocBook 元素
<para>This is a sample {a contrived one at that} ~ it serves no special purpose.</para>

应该转化为
@PP
This is a sample "{"a contrived one at that"}" "~" it serves no special purpose.

我怎样才能用 XSLT 做到这一点?我正在使用 xsltproc ,所以使用 XPath 2.0 函数不是一种选择,而是许多 EXSLT功能可用。

我尝试使用递归模板,该模板将子字符串生成为特殊字符(例如 { ),然后是转义字符序列( "{" ),然后在特殊字符之后的子字符串上调用自身。但是,在尝试替换多个字符时,我很难使其正常工作,并且其中一个字符用于转义序列本身。

最佳答案

In particular, the characters

/ | & { } # @ ~ \ " 

need to be enclosed in double quotes (") so that lout treats them as ordinary characters.



I. 使用 str-map 最容易实现这一点。 模板 FXSL :
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:strmap="strmap"
exclude-result-prefixes="xsl f strmap">
<xsl:import href="str-dvc-map.xsl"/>

<xsl:output method="text"/>

<strmap:strmap/>

<xsl:template match="/">
<xsl:variable name="vMapFun" select="document('')/*/strmap:*[1]"/>
@PP
<xsl:call-template name="str-map">
<xsl:with-param name="pFun" select="$vMapFun"/>
<xsl:with-param name="pStr" select="."/>
</xsl:call-template>
</xsl:template>

<xsl:template name="escape" match="strmap:*" mode="f:FXSL">
<xsl:param name="arg1"/>

<xsl:variable name="vspecChars">/|&amp;{}#@~\"</xsl:variable>

<xsl:variable name="vEscaping" select=
"substring('&quot;', 1 div contains($vspecChars, $arg1))
"/>

<xsl:value-of select=
"concat($vEscaping, $arg1, $vEscaping)"/>
</xsl:template>

</xsl:stylesheet>

当此转换应用于提供的 XML 文档时 :
<para>This is a sample {a contrived one at that} ~ it serves no special purpose.</para>

产生了想要的、正确的结果 :

@PP
这是一个示例“{”一个人为的“}”“~”它没有特殊用途。

二、使用 XSLT 1.0 递归命名模板 :
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/">
@PP
<xsl:call-template name="escape">
<xsl:with-param name="pStr" select="."/>
</xsl:call-template>
</xsl:template>

<xsl:template name="escape">
<xsl:param name="pStr" select="."/>
<xsl:param name="pspecChars">/|&amp;{}#@~\"</xsl:param>

<xsl:if test="string-length($pStr)">
<xsl:variable name="vchar1" select="substring($pStr,1,1)"/>

<xsl:variable name="vEscaping" select=
"substring('&quot;', 1 div contains($pspecChars, $vchar1))
"/>

<xsl:value-of select=
"concat($vEscaping, $vchar1, $vEscaping)"/>

<xsl:call-template name="escape">
<xsl:with-param name="pStr" select="substring($pStr,2)"/>
<xsl:with-param name="pspecChars" select="$pspecChars"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

关于xslt - 如何使用 XSLT 用它们的转义变体替换某些字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3517303/

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