使用XSLT对节点进行交叉评估

时间:2018-05-31 11:44:23

标签: xslt xslt-1.0

当我的SENTENCE[SECONDARY]/TOKEN值大于另一个FURTHER_FROM_PRIMARY="YES"的值时,我正在尝试分析下面的输入XML,用BEGIN标记节点BEGIN。 {1}}在同一个SENTENCE[SECONDARY]/TOKEN节点中。

下面是输入XML:

PARAGRAPH

这是XSLT代码:

<DOCUMENT>
  <SECTION>
    <PARAGRAPH TRACK="0">
      <SENTENCE NAME="PRIMARY" COUNT="1">
        <TOKEN BEGIN="14" END="25" SENTENCE_BEGIN="0" SENTENCE_END="48"/>   
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEFORE_PRIMARY="YES" BEGIN="0" END="9" SENTENCE_BEGIN="0" SENTENCE_END="48" SAME_SENTENCE="YES"/>        
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN  BEGIN="43" END="47" SENTENCE_BEGIN="0" SENTENCE_END="48" SAME_SENTENCE="YES"/>       
       </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEGIN="27" END="34" SENTENCE_BEGIN="0" SENTENCE_END="48" SAME_SENTENCE="YES"/>          
      </SENTENCE>
    </PARAGRAPH>
    <PARAGRAPH TRACK="1">
      <SENTENCE NAME="PRIMARY" COUNT="2">
        <TOKEN BEGIN="37" END="41" SENTENCE_BEGIN="0" SENTENCE_END="48"/> 
        <TOKEN BEGIN="37" END="41" SENTENCE_BEGIN="0" SENTENCE_END="48"/>
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEGIN="43" END="47" SENTENCE_BEGIN="0" SENTENCE_END="48"/> 
      </SENTENCE>
    </PARAGRAPH>
    <PARAGRAPH TRACK="2">
      <SENTENCE NAME="PRIMARY" COUNT="2">
        <TOKEN BEGIN="164" END="170" SENTENCE_BEGIN="135" SENTENCE_END="187"/>          
        <TOKEN BEGIN="164" END="170" SENTENCE_BEGIN="135" SENTENCE_END="187"/>         
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEGIN="172" END="175" SENTENCE_BEGIN="135" SENTENCE_END="187"/>          
      </SENTENCE>
    </PARAGRAPH>
    <PARAGRAPH TRACK="3">
      <SENTENCE NAME="PRIMARY" COUNT="4">
        <TOKEN BEGIN="93" END="97" SENTENCE_BEGIN="50" SENTENCE_END="133"/>   
        <TOKEN BEGIN="93" END="97" SENTENCE_BEGIN="50" SENTENCE_END="133"/>         
        <TOKEN BEGIN="135" END="139" SENTENCE_BEGIN="135" SENTENCE_END="187"/>
        <TOKEN BEGIN="135" END="139" SENTENCE_BEGIN="135" SENTENCE_END="187"/>
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEGIN="141" END="147" SENTENCE_BEGIN="135" SENTENCE_END="187" SAME_SENTENCE="YES"/>        
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEFORE_PRIMARY="YES" BEGIN="79" END="88" SENTENCE_BEGIN="50" SENTENCE_END="133" SAME_SENTENCE="YES"/>          
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="3">
        <TOKEN  BEGIN="110" END="113" SENTENCE_BEGIN="50" SENTENCE_END="133" SAME_SENTENCE="YES"/>          
        <TOKEN  BEGIN="129" END="132" SENTENCE_BEGIN="50" SENTENCE_END="133" SAME_SENTENCE="YES"/>          
        <TOKEN  BEGIN="172" END="175" SENTENCE_BEGIN="135" SENTENCE_END="187" SAME_SENTENCE="YES"/>          
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEGIN="99" END="104" SENTENCE_BEGIN="50" SENTENCE_END="133" SAME_SENTENCE="YES"/> 
        <TOKEN  BEGIN="153" END="158" SENTENCE_BEGIN="135" SENTENCE_END="187" SAME_SENTENCE="YES"/> 
      </SENTENCE>
    </PARAGRAPH>
    <PARAGRAPH TRACK="4">
      <SENTENCE NAME="PRIMARY" COUNT="1">
        <TOKEN BEGIN="119" END="127" SENTENCE_BEGIN="50" SENTENCE_END="133"/>
        </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEGIN="129" END="132" SENTENCE_BEGIN="50" SENTENCE_END="133"/>       
      </SENTENCE>
    </PARAGRAPH>
    <PARAGRAPH TRACK="5">
      <SENTENCE NAME="PRIMARY" COUNT="1">
        <TOKEN BEGIN="50" END="58" SENTENCE_BEGIN="50" SENTENCE_END="133"/>          
      </SENTENCE>
      <SENTENCE NAME="SECONDARY" COUNT="1">
        <TOKEN BEGIN="60" END="66" SENTENCE_BEGIN="50" SENTENCE_END="133"/>   
       </SENTENCE>
    </PARAGRAPH>
  </SECTION>
</DOCUMENT>

这是输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.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="TOKEN[@SAME_SENTENCE][not (@BEFORE_PRIMARY)]">
     <xsl:copy>
      <xsl:if test="((current()/@SENTENCE_BEGIN = ../../SENTENCE[@NAME='PRIMARY']/TOKEN/@SENTENCE_BEGIN) and (current()/@SENTENCE_END = ../../SENTENCE[@NAME='PRIMARY']/TOKEN/@SENTENCE_END)) and  (current()/@BEGIN &gt; ../../SENTENCE[@NAME='SECONDARY']/TOKEN [@SAME_SENTENCE] [not (@BEFORE_PRIMARY)]/@BEGIN)">

            <xsl:attribute name="FURTHER_FROM_PRIMARY">YES</xsl:attribute>

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

</xsl:stylesheet>

不幸的是,在上面的示例中,XSLT代码错误地选择了<?xml version="1.0" encoding="utf-16"?> <DOCUMENT> <SECTION> <PARAGRAPH TRACK="0"> <SENTENCE NAME="PRIMARY" COUNT="1"> <TOKEN BEGIN="14" END="25" SENTENCE_BEGIN="0" SENTENCE_END="48" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN BEFORE_PRIMARY="YES" BEGIN="0" END="9" SENTENCE_BEGIN="0" SENTENCE_END="48" SAME_SENTENCE="YES" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN FURTHER_FROM_PRIMARY="YES" BEGIN="43" END="47" SENTENCE_BEGIN="0" SENTENCE_END="48" SAME_SENTENCE="YES" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN BEGIN="27" END="34" SENTENCE_BEGIN="0" SENTENCE_END="48" SAME_SENTENCE="YES" /> </SENTENCE> </PARAGRAPH> <PARAGRAPH TRACK="1"> <SENTENCE NAME="PRIMARY" COUNT="2"> <TOKEN BEGIN="37" END="41" SENTENCE_BEGIN="0" SENTENCE_END="48" /> <TOKEN BEGIN="37" END="41" SENTENCE_BEGIN="0" SENTENCE_END="48" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN BEGIN="43" END="47" SENTENCE_BEGIN="0" SENTENCE_END="48" /> </SENTENCE> </PARAGRAPH> <PARAGRAPH TRACK="2"> <SENTENCE NAME="PRIMARY" COUNT="2"> <TOKEN BEGIN="164" END="170" SENTENCE_BEGIN="135" SENTENCE_END="187" /> <TOKEN BEGIN="164" END="170" SENTENCE_BEGIN="135" SENTENCE_END="187" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN BEGIN="172" END="175" SENTENCE_BEGIN="135" SENTENCE_END="187" /> </SENTENCE> </PARAGRAPH> <PARAGRAPH TRACK="3"> <SENTENCE NAME="PRIMARY" COUNT="4"> <TOKEN BEGIN="93" END="97" SENTENCE_BEGIN="50" SENTENCE_END="133" /> <TOKEN BEGIN="93" END="97" SENTENCE_BEGIN="50" SENTENCE_END="133" /> <TOKEN BEGIN="135" END="139" SENTENCE_BEGIN="135" SENTENCE_END="187" /> <TOKEN BEGIN="135" END="139" SENTENCE_BEGIN="135" SENTENCE_END="187" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN FURTHER_FROM_PRIMARY="YES" BEGIN="141" END="147" SENTENCE_BEGIN="135" SENTENCE_END="187" SAME_SENTENCE="YES" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN BEFORE_PRIMARY="YES" BEGIN="79" END="88" SENTENCE_BEGIN="50" SENTENCE_END="133" SAME_SENTENCE="YES" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="3"> <TOKEN FURTHER_FROM_PRIMARY="YES" BEGIN="110" END="113" SENTENCE_BEGIN="50" SENTENCE_END="133" SAME_SENTENCE="YES" /> <TOKEN FURTHER_FROM_PRIMARY="YES" BEGIN="129" END="132" SENTENCE_BEGIN="50" SENTENCE_END="133" SAME_SENTENCE="YES" /> <TOKEN FURTHER_FROM_PRIMARY="YES" BEGIN="172" END="175" SENTENCE_BEGIN="135" SENTENCE_END="187" SAME_SENTENCE="YES" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN BEGIN="99" END="104" SENTENCE_BEGIN="50" SENTENCE_END="133" SAME_SENTENCE="YES" /> <TOKEN FURTHER_FROM_PRIMARY="YES" BEGIN="153" END="158" SENTENCE_BEGIN="135" SENTENCE_END="187" SAME_SENTENCE="YES" /> </SENTENCE> </PARAGRAPH> <PARAGRAPH TRACK="4"> <SENTENCE NAME="PRIMARY" COUNT="1"> <TOKEN BEGIN="119" END="127" SENTENCE_BEGIN="50" SENTENCE_END="133" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN BEGIN="129" END="132" SENTENCE_BEGIN="50" SENTENCE_END="133" /> </SENTENCE> </PARAGRAPH> <PARAGRAPH TRACK="5"> <SENTENCE NAME="PRIMARY" COUNT="1"> <TOKEN BEGIN="50" END="58" SENTENCE_BEGIN="50" SENTENCE_END="133" /> </SENTENCE> <SENTENCE NAME="SECONDARY" COUNT="1"> <TOKEN BEGIN="60" END="66" SENTENCE_BEGIN="50" SENTENCE_END="133" /> </SENTENCE> </PARAGRAPH> </SECTION> </DOCUMENT> 之一:

TOKENS

下面是完整的所需输出:

<SENTENCE NAME="SECONDARY" COUNT="1">
     <TOKEN FURTHER_FROM_PRIMARY="YES" BEGIN="141" END="147" SENTENCE_BEGIN="135" SENTENCE_END="187" SAME_SENTENCE="YES"/>        
</SENTENCE>

你对我做错了什么有任何线索吗?

1 个答案:

答案 0 :(得分:0)

您在说明中没有提及您对SENTENCE_BEGINSENTENCE_END的检查也是相同的。

问题在于,BEGIN的检查不依赖于SENTENCE_BEGINSENTENCE_END相同。换句话说,您正在测试&#34;是否存在具有相同SENTENCE_BEGIN的令牌,并且是否存在具有相同SENTENCE_END的令牌,并且是否存在具有较低BEGIN的令牌&#34;

我认为这就是你需要的......

<xsl:if test="../../SENTENCE[@NAME='SECONDARY']/TOKEN[@SENTENCE_BEGIN = current()/@SENTENCE_BEGIN and @SENTENCE_END = current()/@SENTENCE_END and not(@BEFORE_PRIMARY) and current()/@BEGIN &gt; @BEGIN]">