我将拼音和中文字符替换为实体。我有一个xml文件,其中发生Chienese和语音字符我想在发生语音字符时添加<emph type="Chienese" cstyle="Chienese"></emph> element when occuring Chienese character and want <emph type="Phonetic_italic" cstyle="Phonetic_italic"></emph>
。我的问题是只有一个条件适用。请建议。
输入 -
<article>
<para>1789 huldigte Goethe seinem G?nner, dem Fürsten Karl August von Weimar, mit einem kleinen Gedicht, in dem er von seinem <emph type="italic">Werther</emph> in China sprach:</para>
<para>Vgl. Wei, Maop?ing, 2004, S. 74–75. 1914 erschien die Übersetzung von einem kleinen Teil aus <emph type="italic">Werther</emph>. Der Übersetzer M?a Junwu (1881–1940) gehört zu den ersten Gelehrten, die sich für Goethe interessierten. Bei seiner Übersetzung von <emph type="italic">Armins Klagelied am Meeresufer</emph> (???????) handelt es sich abœer noch eher um eine freie Bearbeitung. Diese Übersetzung soll schon zwischen 1902 und 1903 fertiggestellt worden sein, also über zehn Jahre vor der offiziellen Veröffentlichung.</para>
</article>
XSLT -
<xsl:character-map name="m1">
<!-- phonetic character -->
<xsl:output-character character="č" string="&#x010D;"/>
<xsl:output-character character="ɽ" string="&#x027D;"/>
<xsl:output-character character="”" string="&#x201D;"/>
<xsl:output-character character="č" string="&#x010D;"/>
<xsl:output-character character="ħ" string="&#x0127;"/>
<xsl:output-character character="œ" string="&#x0153;"/>
<!-- chinies character -->
<xsl:output-character character="阿" string="&#x963F;"/>
<xsl:output-character character="明" string="&#x660E;"/>
<xsl:output-character character="临" string="&#x4E34;"/>
<xsl:output-character character="岸" string="&#x5CB8;"/>
<xsl:output-character character="哭" string="&#x54ED;"/>
<xsl:output-character character="女" string="&#x5973;"/>
<xsl:output-character character="诗" string="&#x8BD7;"/>
</xsl:character-map>
<xsl:output omit-xml-declaration="no" method="xml" indent="no" encoding="UTF-8"
use-character-maps="m1"/>
<xsl:template match="article">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="para">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()" priority="10">
<xsl:analyze-string select="." regex="([č|ɽ|n”|č|ħ|œ]+)">
<xsl:matching-substring>
<emph type="Phonetic_italic" cstyle="Phonetic_italic">
<xsl:value-of select="."/>
</emph>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="text()" priority="10">
<xsl:analyze-string select="." regex="([|n阿|n明|n临|n岸|n哭|n女|n诗]+)">
<xsl:matching-substring>
<emph type="Chienese" cstyle="Chienese">
<xsl:value-of select="."/>
</emph>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
输出 -
<article>
<para>1789 huldigte Goethe sei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>em G<emph type="Phonetic_italic" cstyle="Phonetic_italic">čnn</emph>er, dem Fürste<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Karl August vo<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Weimar, mit ei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>em
klei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>e<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Gedicht, i<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> dem er vo<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> sei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>em Werther i<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Chi<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>a
sprach:</para>
<para>Vgl. Wei, Maop<emph type="Phonetic_italic" cstyle="Phonetic_italic">ɽ</emph>i<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>g, 2004, S. 74–75. 1914 erschie<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> die
Übersetzu<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>g vo<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> ei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>em klei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>e<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Teil aus Werther. Der
Übersetzer M<emph type="Phonetic_italic" cstyle="Phonetic_italic">ħ</emph>a Ju<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>wu (1881–1940) gehört zu de<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> erste<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Gelehrte<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>, die sich für Goethe
i<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>teressierte<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>. Bei sei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>er Übersetzu<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>g vo<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Armi<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>s Klagelied am
Meeresufer (阿明临岸哭女诗) ha<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>delt es sich ab<emph type="Phonetic_italic" cstyle="Phonetic_italic">œ</emph>er <emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>och eher um ei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>e freie
Bearbeitu<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>g. Diese Übersetzu<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>g soll scho<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> zwische<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> 1902 u<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>d 1903 fertiggestellt worde<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>
sei<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>, also über zeh<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Jahre vor der offizielle<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph> Veröffe<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>tlichu<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>g.</para>
</article>
提前致谢
答案 0 :(得分:2)
我想嵌套你的处理步骤:
<xsl:template match="text()" priority="10">
<xsl:analyze-string select="." regex="([č|ɽ|n”|č|ħ|œ]+)">
<xsl:matching-substring>
<emph type="Phonetic_italic" cstyle="Phonetic_italic">
<xsl:value-of select="."/>
</emph>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="([|n阿|n明|n临|n岸|n哭|n女|n诗]+)">
<xsl:matching-substring>
<emph type="Chienese" cstyle="Chienese">
<xsl:value-of select="."/>
</emph>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
或者您可以将代码分解为可调用的函数或模板,然后执行字符串分析。
或者在XSLT 3中(模板可以匹配原始数据类型,如您在non-matching-substring
分支中作为上下文项获得的字符串),您可以使用
<xsl:template match="text()">
<xsl:analyze-string select="." regex="([č|ɽ|n”|č|ħ|œ]+)">
<xsl:matching-substring>
<emph type="Phonetic_italic" cstyle="Phonetic_italic">
<xsl:value-of select="."/>
</emph>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:apply-templates select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match=".[. instance of xs:string]">
<xsl:analyze-string select="." regex="([|n阿|n明|n临|n岸|n哭|n女|n诗]+)">
<xsl:matching-substring>
<emph type="Chienese" cstyle="Chienese">
<xsl:value-of select="."/>
</emph>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>