如何在XSLT 2.0中将单个字符包装在元素中

时间:2018-02-02 07:07:48

标签: xml xslt xslt-2.0

我正在进行图书转换,其中一些实体不支持字体,所以我想在包装器中创建特定实体并处理这些实体以更改字体样式:

INPUT XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <child1>This is simple para.</child1>
    <child1>This is &#x02bf; para.</child1>
    <child1>This is simple para.</child1>
    <child1>This is ʿ para.</child1>
</root>

CURRENT OUT:

<?xml version="1.0" encoding="US-ASCII"?>
<root>
    <child1>This is simple para.</child1>
    <child1>
      <emph cstyle="Phonetic" type="Phonetic">This is &#x2bf; para.</emph>
   </child1>
    <child1>This is simple para.</child1>
    <child1>
      <emph cstyle="Phonetic" type="Phonetic">This is &#x2bf; para.</emph>
   </child1>
</root>

预期输出

<?xml version="1.0" encoding="US-ASCII"?>
<root>
    <child1>This is simple para.</child1>
    <child1>This is <emph cstyle="Phonetic" type="Phonetic">&#x2bf;</emph> para.</child1>
    <child1>This is simple para.</child1>
    <child1>This is <emph cstyle="Phonetic" type="Phonetic">&#x2bf;</emph> para.</child1>
</root>

使用XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs" version="2.0">

    <xsl:output method="xml" indent="yes" encoding="US-ASCII"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="//text()[contains(normalize-space(.), '&#x02bf;')]">
        <emph cstyle="Phonetic" type="Phonetic">
            <xsl:value-of select="."/>
        </emph>
    </xsl:template>


</xsl:stylesheet>

0 个答案:

没有答案