输入:
<chapter xmlns='http://www.w3.org/1998/Math/MathML'>
<math>
<mtext mathcolor="#3e9a3c">This is sample 1</mtext>
<mtext mathcolor="#009bd2">This is sample 2</mtext>
</math>
</chapter>
输出:
<?xml version='1.0' encoding='UTF-8' ?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML"><math>~COLOR~[Green]This is sample 1~COLOR~[Red]This is sample 2</math></chapter>
配置文件:
<?xml version="1.0"?>
<colors>
<color><mathcolor>#3e9a3c</mathcolor><textcolor>Green</textcolor><colorvalue>1</colorvalue></color>
<color><mathcolor>#009bd2</mathcolor><textcolor>Red</textcolor><colorvalue>2</colorvalue></color>
</colors>
XSLT尝试过:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:mtext">
<xsl:if test="@mathcolor">
<xsl:choose>
<xsl:when test="@mathcolor='#3e9a3c'"><xsl:text disable-output-escaping="yes">~COLOR~[Green]</xsl:text></xsl:when>
<xsl:when test="@mathcolor='#009bd2'"><xsl:text disable-output-escaping="yes">~COLOR~[Red]</xsl:text></xsl:when>
</xsl:choose>
</xsl:if>
<xsl:apply-templates/></xsl:template>
</xsl:stylesheet>
我需要根据配置文件将上面给出的输入转换为所需的输出。如果用户更新配置文件,并且输入具有mathcolor属性值,则应该如输出中所示转换其对应的颜色。我可以使用XSLT 1.0。请帮助解决这个问题
例如:
如果用户在配置文件中添加以下编码:
<color><mathcolor>#007c62</mathcolor><textcolor>Magenta</textcolor><colorvalue>3</colorvalue></color>
和输入包含mathcolor =“#007c62”,然后应在输出中应用洋红色。
答案 0 :(得分:1)
这应该有效:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.w3.org/1998/Math/MathML">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>
<!--
Config file name/path, can be passed as a parameter to XSL transformation
-->
<xsl:param name="config.file" select="'config.xml'"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- <m:mtext> elements with a @matcholor attribute, no need for <xsl:if> -->
<xsl:template match="m:mtext[@mathcolor]">
<xsl:text>~COLOR~[</xsl:text>
<!--
Fetch color name from $config.file (<color> element with a <mathcolor>
child that has the same value as the @mathcolor attribute of the element
currently being processed)
-->
<xsl:value-of
select="document($config.file)/colors/color
[mathcolor = current()/@mathcolor]/textcolor"/>
<xsl:text>]</xsl:text>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
<chapter xmlns='http://www.w3.org/1998/Math/MathML'>
<math>
<mtext mathcolor="#3e9a3c">This is sample 1</mtext>
<mtext mathcolor="#009bd2">This is sample 2</mtext>
</math>
</chapter>
<?xml version="1.0"?>
<colors>
<color>
<mathcolor>#3e9a3c</mathcolor>
<textcolor>Green</textcolor>
<colorvalue>1</colorvalue>
</color>
<color>
<mathcolor>#009bd2</mathcolor>
<textcolor>Red</textcolor>
<colorvalue>2</colorvalue>
</color>
</colors>
<?xml version="1.0" encoding="UTF-8"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<math>~COLOR~[Green]This is sample 1~COLOR~[Red]This is sample 2</math>
</chapter>