我有一個文本節點,其中包含 7 位 ASCII 文本以及更高的 unicode 字符(例如 x2011、xF0B7、x25CF ...)
我需要能夠(有效地)將這些單個高 unicode 字符轉換為處理指令
例如
‑ -> <processing-instruction name="xxx">character output="hyphen"</pro...>
 -> <processing-instruction name="xxx">character output="page"</pro...>
我嘗試使用xsl:tokenizewhich 在第一個標記分隔符之前/之后拆分文本(例如 x2011),但我最終得到一個包含'text...<processing-instruction>...</processing-instruction'...text'which 跳閘下一個的變數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)。
uj5u.com熱心網友回復:
如果您要查找的字符是已知且有限的,我會列出它們,例如,您設定的函式<xsl:template match="text()"><xsl:analyze-string select="." regex="‑●"><xsl:matching-substring><xsl:processing-instruction name="xxp" select="mf:map(.)"/></xsl:matching-substring><xsl:non-matching-substring><xsl:value-of select="."/></xsl:non-matching-substring></xsl:analyze-string></xsl:template>在哪里將每個字符映射到您想要作為 pi 資料輸出的字串。mf:map在 XSLT 3 中,我可能會將字符到名稱的映射存盤在 XPath/XSLT 映射中,在 XSLT 2 中,您可以使用 some xsl:paramor xsl:variableeg<xsl:param name="characters-to-name"><map char="‑">bxhyphen</map>...</xsl:param>并在需要時選擇其中,甚至通過設定鍵。
XSLT/XPath/XQuery 世界中的全新是最近發布的不可見 XML 規范 ( https://invisiblexml.org/ ),具有各種(尚未完善)實作;使用不可見的 XML,您可以為您的格式定義一個語法,然后處理器只使用該語法將其轉換為 XML,您可以像往常一樣使用 XSLT/XPath/XQuery 進行處理。
所以用例如語法
text: (ascii | hyphen | page | bub)*.
-ascii: ["a"-"z"; "A"-"Z"; "0"-"9"].
hyphen: #2011.
page: #F0B7.
bub: #25CF.
不可見的 XML 處理器會將 eg 的輸入轉換為您可以處理A?B的 XML <text>A<hyphen>?</hyphen>B</text>,然后使用 XSLT 進一步創建任何專用輸出,例如處理您想要的指令。
uj5u.com熱心網友回復:
沒有的版本xsl:analyze-string如下。它使用一個單獨的檔案來存盤代碼點/字串關系。
因此,在此示例中,一個名為的檔案codes.xml包含映射(必須先將十六進制值轉換為十進制 - 這里已經完成):
<CharKey>
<Char cp="8209" string="hyphen" />
<Char cp="61623" string="page" />
</CharKey>
并且樣式表(這里是 XSLT-3.0,但它也可以與 XSLT-2.0 一起使用,并進行一些小的修改)迭代字串的代碼點:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:variable name="codes" select="document('codes.xml')/CharKey" />
<!-- text() node is matched here -->
<xsl:template match="/Record/Text">
<xsl:variable name="cps" select="string-to-codepoints(.)" />
<xsl:for-each select="$cps">
<xsl:variable name="curCP" select="$codes/Char[@cp=current()]" />
<xsl:choose>
<xsl:when test="$curCP"><xsl:processing-instruction name="xxx" expand-text="yes">character output="{$curCP/@string}"</xsl:processing-instruction></xsl:when>
<xsl:otherwise><xsl:value-of select="codepoints-to-string(.)" /></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
這可以進一步簡化,但作為一個例子,它應該可以作業。
樣品的
<Record>
<Text>Hello‑End</Text>
</Record>
輸出是
Hello<?xxx character output="hyphen"?><?xxx character output="page"?>End
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/490155.html
