gpt4 book ai didi

基于配置文件的 XSLT 转换

转载 作者:行者123 更新时间:2023-12-04 05:03:52 30 4
gpt4 key购买 nike

输入:

<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>

我需要根据配置文件将上面给出的输入转换为所需的输出。如果用户更新了配置文件,并且如果 Input 具有 mathcolor 属性值,则其对应的颜色应转换为如 output 所示。我可以使用 XSLT 1.0。请帮助解决这个问题

例如:
如果用户在配置文件中添加如下代码: <color><mathcolor>#007c62</mathcolor><textcolor>Magenta</textcolor><colorvalue>3</colorvalue></color>并且输入包含 mathcolor="#007c62",那么 Magenta 应该应用在输出中。

最佳答案

这应该有效:
样式表

<?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 @mathcolor 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>

关于基于配置文件的 XSLT 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15760051/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com