我正在进行图书转换,其中一些实体不支持字体,所以我想在包装器中创建特定实体并处理这些实体以更改字体样式:
INPUT XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child1>This is simple para.</child1>
<child1>This is ʿ 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 ʿ para.</emph>
</child1>
<child1>This is simple para.</child1>
<child1>
<emph cstyle="Phonetic" type="Phonetic">This is ʿ 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">ʿ</emph> para.</child1>
<child1>This is simple para.</child1>
<child1>This is <emph cstyle="Phonetic" type="Phonetic">ʿ</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(.), 'ʿ')]">
<emph cstyle="Phonetic" type="Phonetic">
<xsl:value-of select="."/>
</emph>
</xsl:template>
</xsl:stylesheet>