2014-11-15 16 views
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> 

回答

2

下面是使用XSLT 2.0我的建議:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:output indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="math[descendant::mspace]"> 
    <xsl:copy> 
    <xsl:variable name="max-elements" as="element(mspace)*"> 
     <xsl:for-each-group select="descendant::*[not(*)]" group-adjacent="boolean(self::mspace)"> 
     <xsl:if test="current-grouping-key()"> 
      <xsl:sequence select="current-group()[. = max(current-group())][last()]"/> 
     </xsl:if> 
     </xsl:for-each-group> 
    </xsl:variable> 
    <xsl:apply-templates select="@*"/> 
    <xsl:apply-templates select="node()"> 
     <xsl:with-param name="max-elements" select="$max-elements" tunnel="yes"/> 
    </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="mspace"> 
    <xsl:param name="max-elements" tunnel="yes"/> 
    <xsl:choose> 
    <xsl:when test=". intersect $max-elements"> 
     <xsl:next-match/> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:comment select="."/> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

</xsl:stylesheet> 

它將您發佈的樣品轉換成

<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> 
     <mtext>log</mtext> 
     <mn>3</mn> 
     <!--2--> 
     </mrow> 
     <mspace>2</mspace> 
    </math> 
    <math><!--3--> 
     <mspace>3</mspace> 
     <mn>4</mn> 
     <mo>-</mo> 
     <mi>a</mi> 
     <mspace>2</mspace> 
    </math> 
</article> 
+0

先生,您的幫助很大。工作100%。 –

+0

請解釋>編碼。 –

+1

'select'表達式'descendant :: * [not(*)]'選擇沒有任何子元素的所有後代元素,所以像mspace或mn這樣的元素包含數據而不是其他元素。 'group-adjacent'表達式boolean(self :: mspace)允許我們識別相鄰的'mspace'元素,分組鍵對於相鄰的'mspace'元素爲真,對於所有其他元素則爲false。 –

相關問題