0
请建议,如何到达内m /莫包含括号。如何到达相同名称和格式的嵌套子元素
在给定的样本中,当mrow(如果它的第一个和最后一个孩子应该是带有大括号的'MO'),第一级大括号被转换为'MFENCED'。但无法修改二级括号,其中那些也有'MFRAC'作为后代。模板匹配应该来自'MROW'(如给出)。
XML输入:
<article>
<body>
<math id="m1">
<mrow>
<mo>(</mo><!--first level braces open-->
<mi>u</mi>
<mo>+</mo>
<mi>g</mi>
<mi>=</mi>
<mrow>
<mo>(</mo><!--second level braces open-->
<mfrac>
<mrow><mn>1</mn></mrow>
<mrow><mn>2</mn></mrow>
</mfrac>
<mo>)</mo><!--second level braces close-->
</mrow>
<mo>)</mo><!--first level braces close-->
</mrow>
</math>
<math id="m2">
<mrow>
<mo>(</mo>
<mrow>
<mfrac>
<mn>8</mn>
<mn>9</mn>
</mfrac>
</mrow>
<mo>)</mo>
</mrow>
</math>
</body>
</article>
XSLT 2.0:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="node()|@*">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="mrow[matches(child::*[1][name()='mo'], '^(\(|\[|\{)$')]
[matches(child::*[position()=last()][name()='mo'], '^(\)|\]|\})$')]">
<xsl:choose>
<xsl:when test="descendant::mfrac">
<xsl:copy>
<xsl:element name="mfenced">
<xsl:attribute name="open"><xsl:value-of select="child::*[1][name()='mo']"/></xsl:attribute>
<xsl:attribute name="close"><xsl:value-of select="child::*[position()=last()][name()='mo']"/></xsl:attribute>
<xsl:for-each select="*">
<xsl:if test="position()=1"/>
<xsl:if test="not(position()=1) and not(position()=last())">
<xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>
</xsl:if>
<xsl:if test="position()=last()"/>
</xsl:for-each>
</xsl:element>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
要求的结果:
<article>
<body>
<math id="m1">
<mrow>
<mfenced open="(" close=")"><mi>u</mi><mo>+</mo><mi>g</mi><mi>=</mi>
<mrow>
<mfenced open="(" close=")"><!-- this node or modification required, because, within this MFRAC presents, then it should convert to 'MFENCED' -->
<mfrac>
<mrow><mn>1</mn></mrow>
<mrow><mn>2</mn></mrow>
</mfrac>
</mfenced>
</mrow>
</mfenced>
</mrow>
</math>
<math id="m2">
<mrow>
<mfenced open="(" close=")">
<mrow>
<mfrac>
<mn>8</mn>
<mn>9</mn>
</mfrac>
</mrow>
</mfenced>
</mrow>
</math>
</body>
</article>
感谢您的建议,加上一个不错的分组方法。 –
有一个疑问先生,为什么第二个MROW也是一个孩子,为什么它没有在-for-each-select中达到=“*”。请建议。 –
您的问题与我的XSLT代码有关吗?还是问你的问题?如果您有我的建议无效的输入样本,请考虑编辑您的问题并显示它。 –