Проблема сущностей при конвертации через xslt - PullRequest
0 голосов
/ 31 мая 2018

Я заменяю фонетический и китайский иероглиф на сущность.У меня есть XML-файл, в котором встречаются китайский и фонетический символы, которые я хочу добавить <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="&amp;#x010D;"/>
        <xsl:output-character character="ɽ" string="&amp;#x027D;"/>
        <xsl:output-character character="”" string="&amp;#x201D;"/>
        <xsl:output-character character="č" string="&amp;#x010D;"/>
        <xsl:output-character character="ħ" string="&amp;#x0127;"/>
        <xsl:output-character character="œ" string="&amp;#x0153;"/>
        <!-- chinies character -->                  
        <xsl:output-character character="阿" string="&amp;#x963F;"/>
        <xsl:output-character character="明" string="&amp;#x660E;"/>
        <xsl:output-character character="临" string="&amp;#x4E34;"/>
        <xsl:output-character character="岸" string="&amp;#x5CB8;"/>
        <xsl:output-character character="哭" string="&amp;#x54ED;"/>
        <xsl:output-character character="女" string="&amp;#x5973;"/>
        <xsl:output-character character="诗" string="&amp;#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="([&#x010D;|&#x027D;|n&#x201D;|&#x010D;|&#x0127;|&#x0153;]+)">
            <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&#x963F;|n&#x660E;|n&#x4E34;|n&#x5CB8;|n&#x54ED;|n&#x5973;|n&#x8BD7;]+)">
            <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">&#x010D;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">&#x027D;</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">&#x0127;</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 (&#x963F;&#x660E;&#x4E34;&#x5CB8;&#x54ED;&#x5973;&#x8BD7;) ha<emph type="Phonetic_italic" cstyle="Phonetic_italic">n</emph>delt es sich ab<emph type="Phonetic_italic" cstyle="Phonetic_italic">&#x0153;</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>

Заранее спасибо

1 Ответ

0 голосов
/ 31 мая 2018

Полагаю, что для выполнения ваших шагов обработки потребуется:

<xsl:template match="text()" priority="10">
    <xsl:analyze-string select="." regex="([&#x010D;|&#x027D;|n&#x201D;|&#x010D;|&#x0127;|&#x0153;]+)">
        <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&#x963F;|n&#x660E;|n&#x4E34;|n&#x5CB8;|n&#x54ED;|n&#x5973;|n&#x8BD7;]+)">
        <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="([&#x010D;|&#x027D;|n&#x201D;|&#x010D;|&#x0127;|&#x0153;]+)">
        <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&#x963F;|n&#x660E;|n&#x4E34;|n&#x5CB8;|n&#x54ED;|n&#x5973;|n&#x8BD7;]+)">
        <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>
...