0
請建議保留最大值有價值的mspace連續查找mspaces [如果連續發現文本包含元素[mspace]]中間有時可能有其他像暮光之城這樣的元素將出現,在這種情況下,連續的文字中只有空格才能被考慮。看到我需要快速評論的評論。如何保留最大值mspace,如果連續發現其他mspaces
輸入XML:
<article>
<math>
<mspace>3</mspace>
<mrow>
<mspace>3</mspace>
<mspace>2</mspace>
<mo>(</mo>
<mo>+</mo>
<mo>)</mo>
<mspace>3</mspace>
</mrow>
<mspace>9</mspace>
</math>
<math>
<mo>[</mo>
<mrow>
<mspace>3</mspace>
<mspace>2</mspace>
<mspace>3</mspace>
<mtext>log</mtext>
<mn>3</mn>
<mspace>2</mspace>
</mrow>
<mspace>2</mspace>
</math>
<math>
<mspace>3</mspace>
<mspace>3</mspace>
<mn>4</mn>
<mo>-</mo>
<mi>a</mi>
<mspace>2</mspace>
</math>
</article>
XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="mspace">
<xsl:variable name="var1" select="following::text()[normalize-space(.)!=''][1][generate-id(ancestor::math)=generate-id(current()/ancestor::math)]"/>
<xsl:variable name="var2" select="preceding::text()[normalize-space(.)!=''][1][generate-id(ancestor::math)=generate-id(current()/ancestor::math)]"/>
<xsl:variable name="var1a" select="following::text()[normalize-space(.)!=''][2][generate-id(ancestor::math)=generate-id(current()/ancestor::math)]"/>
<xsl:variable name="var2a" select="preceding::text()[normalize-space(.)!=''][2][generate-id(ancestor::math)=generate-id(current()/ancestor::math)]"/>
<xsl:variable name="vPresent" select="."/>
<xsl:variable name="vMax" select="max(($var1, $var2, $var1a, $var2a, $vPresent))"/>
<xsl:choose>
<xsl:when test="$vPresent lt $var1 or $vPresent lt $var1a">
<xsl:comment><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:comment>
</xsl:when>
<xsl:when test="$vPresent lt $var2 or $vPresent lt $var2a">
<xsl:comment><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:comment>
</xsl:when>
<xsl:when test="$vPresent eq $var2 and $vPresent eq $var1a">
<xsl:comment><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:comment>
</xsl:when>
<xsl:when test="not($vPresent eq $var2) and $vPresent eq $var1">
<xsl:comment><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:comment>
</xsl:when>
<xsl:otherwise><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
所需的輸出:
<?xml version="1.0" encoding="UTF-8"?><article>
<math>
<!--3-->
<mrow>
<mspace>3</mspace>
<!--2-->
<mo>(</mo>
<mo>+</mo>
<mo>)</mo>
<!--3-->
</mrow>
<mspace>9</mspace>
</math>
<math>
<mo>[</mo>
<mrow>
<!--3-->
<!--2-->
<mspace>3</mspace><!--To be required-->
<mtext>log</mtext>
<mn>3</mn>
<mspace>4</mspace><!--To be required-->
</mrow>
<!--2-->
</math>
<math>
<!--3-->
<mspace>3</mspace><!--To be required-->
<mn>4</mn>
<mo>-</mo>
<mi>a</mi>
<mspace>2</mspace><!--To be required-->
</math>
</article>
先生,您的幫助很大。工作100%。 –
請解釋>編碼。 –
'select'表達式'descendant :: * [not(*)]'選擇沒有任何子元素的所有後代元素,所以像mspace或mn這樣的元素包含數據而不是其他元素。 'group-adjacent'表達式boolean(self :: mspace)允許我們識別相鄰的'mspace'元素,分組鍵對於相鄰的'mspace'元素爲真,對於所有其他元素則爲false。 –