【发布时间】:2022-10-20 10:23:50
【问题描述】:
我有一个文本节点,其中包含 7 位 ASCII 文本以及更高的 unicode 字符(例如 x2011、xF0B7、x25CF ...)
我需要能够(有效地)将这些单个高 unicode 字符转换为处理指令
例如
‑ -> <processing-instruction name="xxx">character output="hyphen"</pro...>
 -> <processing-instruction name="xxx">character output="page"</pro...>
我试过使用xsl:tokenize,它确实在第一个标记分隔符(例如x2011)之前/之后分割文本,但我最终得到一个包含'text...<processing-instruction>...</processing-instruction'...text'的变量,它会触发下一个xsl:token。
我设法采用了以下方法来工作,但它看起来真的很不优雅,我确信有一种更有效/更好的方法可以做到这一点,但我还没有找到任何有效或更好的方法。
第一个字符替换很容易,使用replace(),因为我只是转义%(目标软件使用'%' 来表示其他东西,所以需要以这种方式转义)。
是的,这适用于 x2011-to-< ... >,但最初的目的是直接转换为处理指令。
<xsl:template match="text()">
<xsl:variable name="SR1">
<xsl:value-of select="fn:replace(., '%', '\\%')"/>
</xsl:variable>
<!-- unbreakable hyphen -->
<xsl:variable name="SR2">
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="$SR1"/>
<xsl:with-param name="delimiter">‑</xsl:with-param>
<xsl:with-param name="PI"><xsl:text><?xpp character symbol="bxhyphen" hex="x2011" data="E28091"?></xsl:text></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<!-- page ref -->
<xsl:variable name="SR3">
<xsl:call-template name="tokenize">
<xsl:with-param name="string" ><xsl:copy-of select="$SR2"/></xsl:with-param>
<xsl:with-param name="delimiter"></xsl:with-param>
<xsl:with-param name="PI"><xsl:text><?xpp character symbol="pgref" hex="xF0B7" data="EF82B7"?></xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<!-- page ref -->
<xsl:variable name="SR4">
<xsl:call-template name="tokenize">
<xsl:with-param name="string" ><xsl:copy-of select="$SR3"/></xsl:with-param>
<xsl:with-param name="delimiter">●</xsl:with-param>
<xsl:with-param name="PI"><xsl:text><?xpp character symbol="bub" hex="x25CF" data="E2978F"?></xsl:text>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:copy-of select="$SR4"/>
</xsl:template>
理想情况下,我的目标是有一个“对”列表、十六进制 unicode 及其匹配的处理指令,但任何更好的解决方案将不胜感激!
另一个功能是标记尚未处理的字符,因此 x00-x1F、xFF+ 范围内的任何字符(不包括 x2011、x25CF xF0B7)。
【问题讨论】:
-
如果您想处理文本以创建节点,那么使用 XSLT 2/3
xsl:analyze-string(或在 XSLT 3 中的analyze-string函数)通常是可行的方法。结果中的处理指令通常使用xsl:processing-instruction创建,不清楚为什么使用一些xsl:text。 -
另外,
xsl:token是什么?某些 XSLT 扩展的一部分? -
哎呀。应该是 xsl:tokenize ;)
-
在早期版本中,我确实使用了适用于第一个字符的分析字符串(例如 x2011)。我有:``` <xsl:variable name="SR2"> <xsl:analyze-string select="$SR1" regex="^(.*)‑(.*)$"> <xsl:匹配子字符串> <xsl:value-of select="regex-group(1)"/> <xsl:processing-instruction> ... </processing-instruction> <xsl:value-of select="regex-group (2)"/> </xsl:analyze-string> </xsl:variable> ``` 但是当我用同样的方法定义$SR3,在analyze-string/select中调用$SR2时,却没有' t 输出 $SR2 中包含的处理指令。
标签: xml xslt xslt-2.0 invisible-xml