我想從<mo>
得到改造(</mo>
直到<mo>
)</mo>
到 .. </mfenced>
更改兩個元素成單元素。下面 採樣輸入給出:使用XSLT
示例XML:
<?xml version="1.0" encoding="UTF-8"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<p>
<math>
<mi>sin</mi>
<mo>(</mo>
<mi>x</mi>
<mi>y</mi>
<mo>)</mo>
<mo>=</mo>
<mi>sin</mi>
<mi>x</mi>
<mi>sin</mi>
<mi>y</mi>
</math>
</p>
</chapter>
XSLT 2.0嘗試:需要
<?xml version='1.0'?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns="http://www.w3.org/1998/Math/MathML"
exclude-result-prefixes="m">
<xsl:output method="xml"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:mo">
<xsl:if test="(.)='('">
<mfenced><xsl:apply-templates select="following-sibling::*[(.)=')']" mode="copy"/>
</mfenced>
</xsl:if>
<xsl:if test="(.)=')'"></xsl:if>
</xsl:template>
<xsl:template match="m:mo" mode="copy"/>
</xsl:stylesheet>
輸出:
<?xml version="1.0" encoding="UTF-8"?>
<chapter xmlns="http://www.w3.org/1998/Math/MathML">
<p>
<math>
<mi>sin</mi>
<mfenced>
<mi>x</mi>
<mi>y</mi>
</mfenced>
<mo>=</mo>
<mi>sin</mi>
<mi>x</mi>
<mi>sin</mi>
<mi>y</mi>
</math>
</p>
</chapter>
我是對的,如果我認爲在最終的XML大括號可以嵌套?像例如'cos(sin(x)* y)'? –
@ hr_117:所有的開合花括號及其中的元素都應該組合在元素中,並且它在哪裏出現並不重要。 –
siva2012
但是,如果在仍然打開的大括號中允許打開大括號,它會使可能的解決方案有所不同。哪個應該比鉛太嵌套mfenced。比只適用於上述示例的解決方案不會真的有很大的幫助。 –