如何使用xpath测试多个节点的条件

时间:2020-03-15 10:52:35

标签: xml xpath xslt-1.0

我需要测试一个节点内几个节点的条件

<Snoopy revision="0" version="0">
  <pnml>
    <net id="1" name="contenda" type="IOPT">
      <input/>
      <output/>
      <variable/>
      <place id="2">
        <name>
          <text>pl_2</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>1</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>2</text>
        </bound>
        <graphics>
          <position page="1" x="140" y="160"/>
        </graphics>
      </place>
      <place id="3">
        <name>
          <text>pl_3</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>1</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>3</text>
        </bound>
        <graphics>
          <position page="1" x="260" y="160"/>
        </graphics>
      </place>
      <place id="4">
        <name>
          <text>pl_4</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>1</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>3</text>
        </bound>
        <graphics>
          <position page="1" x="370" y="160"/>
        </graphics>
      </place>
      <place id="5">
        <name>
          <text>pl_5</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>1</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>2</text>
        </bound>
        <graphics>
          <position page="1" x="500" y="150"/>
        </graphics>
      </place>
      <place id="6">
        <name>
          <text>pl_6</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>0</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>3</text>
        </bound>
        <graphics>
          <position page="1" x="200" y="320"/>
        </graphics>
      </place>
      <place id="7">
        <name>
          <text>pl_7</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>0</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>3</text>
        </bound>
        <graphics>
          <position page="1" x="310" y="320"/>
        </graphics>
      </place>
      <place id="8">
        <name>
          <text>pl_8</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>0</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>3</text>
        </bound>
        <graphics>
          <position page="1" x="420" y="320"/>
        </graphics>
      </place>
      <transition id="9">
        <name>
          <text>tr_9</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <priority>1</priority>
        <signalInputGuards/>
        <inputEvents/>
        <graphics>
          <position page="1" x="200" y="240"/>
        </graphics>
      </transition>
      <transition id="10">
        <name>
          <text>tr_10</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <priority>3</priority>
        <signalInputGuards/>
        <inputEvents/>
        <graphics>
          <position page="1" x="300" y="240"/>
        </graphics>
        <outputEvents/>
      </transition>
      <transition id="11">
        <name>
          <text>tr_11</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <priority>2</priority>
        <signalInputGuards/>
        <inputEvents/>
        <graphics>
          <position page="1" x="430" y="240"/>
        </graphics>
        <outputEvents/>
      </transition>
      <arc id="12" source="2" target="9">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="8" y="0"/>
          </graphics>
          <value>2</value>
        </inscription>
      </arc>
      <arc id="13" source="3" target="9">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="7" y="12"/>
          </graphics>
          <value>2</value>
        </inscription>
      </arc>
      <arc id="14" source="3" target="10">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="9" y="2"/>
          </graphics>
          <value>3</value>
        </inscription>
      </arc>
      <arc id="15" source="4" target="10">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="7" y="11"/>
          </graphics>
          <value>2</value>
        </inscription>
      </arc>
      <arc id="16" source="4" target="11">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="9" y="2"/>
          </graphics>
          <value>2</value>
        </inscription>
      </arc>
      <arc id="17" source="5" target="11">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="7" y="11"/>
          </graphics>
          <value>2</value>
        </inscription>
      </arc>
      <arc id="18" source="9" target="6">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="10" y="5"/>
          </graphics>
          <value>2</value>
        </inscription>
      </arc>
      <arc id="19" source="10" target="7">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="10" y="5"/>
          </graphics>
          <value>1</value>
        </inscription>
      </arc>
      <arc id="20" source="11" target="8">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="9" y="4"/>
          </graphics>
          <value>1</value>
        </inscription>
      </arc>
      <transition id="41">
        <name>
          <text>tr_41</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <priority>4</priority>
        <signalInputGuards/>
        <inputEvents/>
        <graphics>
          <position page="1" x="570" y="230"/>
        </graphics>
        <outputEvents/>
      </transition>
      <place id="42">
        <name>
          <text>pl_42</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>0</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>2</text>
        </bound>
        <graphics>
          <position page="1" x="630" y="150"/>
        </graphics>
      </place>
      <place id="43">
        <name>
          <text>pl_43</text>
          <graphics>
            <offset x="-10" y="20"/>
          </graphics>
        </name>
        <comment>
          <text/>
          <graphics>
            <offset x="-30" y="20"/>
          </graphics>
        </comment>
        <initialMarking>
          <text>0</text>
          <graphics>
            <offset x="0" y="-1"/>
          </graphics>
        </initialMarking>
        <bound>
          <text>3</text>
        </bound>
        <graphics>
          <position page="1" x="570" y="320"/>
        </graphics>
      </place>
      <arc id="44" source="5" target="41">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="5" y="-3"/>
          </graphics>
          <value>2</value>
        </inscription>
      </arc>
      <arc id="45" source="42" target="41">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="8" y="11"/>
          </graphics>
          <value>2</value>
        </inscription>
      </arc>
      <arc id="46" source="41" target="43">
        <type>normal</type>
        <graphics/>
        <inscription>
          <graphics>
            <offset page="1" x="10" y="5"/>
          </graphics>
          <value>1</value>
        </inscription>
      </arc>
      <conflict id="63">
        <group id="0">
          <g_priority>1</g_priority>
          <g_transition id="9"/>
          <g_place id="2">
            <g_arc id="12"/>
          </g_place>
          <g_place id="3">
            <g_arc id="13"/>
          </g_place>
        </group>
        <group id="1">
          <g_priority>2</g_priority>
          <g_transition id="11"/>
          <g_place id="4">
            <g_arc id="16"/>
          </g_place>
          <g_place id="5">
            <g_arc id="17"/>
          </g_place>
        </group>
        <group id="2">
          <g_priority>3</g_priority>
          <g_transition id="10"/>
          <g_place id="3">
            <g_arc id="14"/>
          </g_place>
          <g_place id="4">
            <g_arc id="15"/>
          </g_place>
        </group>
        <group id="3">
          <g_priority>4</g_priority>
          <g_transition id="41"/>
          <g_place id="5">
            <g_arc id="44"/>
          </g_place>
          <g_place id="42">
            <g_arc id="45"/>
          </g_place>
        </group>
      </conflict>
    </net>
  </pnml>
</Snoopy>

  xmlns:math="http://exslt.org/math" xmlns:exslt="http://exslt.org/common"
 extension-element-prefixes="math exslt MSXML">


<xsl:key name="arc" match="arc" use="@id" />
<xsl:key name="source" match="arc" use="@source" />
<xsl:key name="conflict" match="conflict" use="@id" />
<xsl:key name="transition" match="transition" use="@id" />
<xsl:key name="place" match="place" use="@id" />

<xsl:template name="proc-conflict" match="conflict">
 <xsl:variable name="conflict_id" select="@id" />

   <xsl:for-each select="key('conflict', $conflict_id)/group">
   <xsl:sort select="g_priority" order="ascending" data-type="number" />
     <xsl:variable name="g_priority" select="./g_priority" />   
       <xsl:choose>
         <xsl:when test="position() = '1'">
         <xsl:text>          Iffff </xsl:text>
         </xsl:when>
        <xsl:otherwise>
         <xsl:choose>  
            <xsl:when test="preceding-sibling::group/g_place/@id = current()/g_place/@id and key('place', preceding-sibling::group/g_place[@id = current()/g_place/@id]/@id)/bound/text &lt; (sum(key('arc', preceding-sibling::group/g_place[@id = current()/g_place/@id]/g_arc/@id)/inscription/value)+key('arc', current()/g_place[@id=../preceding-sibling::group/g_place/@id]/g_arc/@id)/inscription/value)">   
               <xsl:text>          Elsif </xsl:text>
            </xsl:when>   
             <xsl:otherwise>
              <xsl:text>          End If;&#10;</xsl:text> 
              <xsl:text>&#10;</xsl:text>
                <xsl:text>          If </xsl:text>
             </xsl:otherwise>
            </xsl:choose>
         <!-- </xsl:for-each> -->     
        </xsl:otherwise>
        </xsl:choose>


         </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

想法如下:对于组内的每个g_place节点,我需要找到具有相同g_place id的组兄弟姐妹。然后,我得到g_arc id并在xml上搜索该弧的ID以获取'inscription'值。我还需要每个g_place的“绑定”值和当前g_place / g_arc的“题字”值。我需要做一个条件,其中g_place bound小于g_arc铭文值与当前弧的铭文值之和。示例:组ID = 2的g_place ID = 3和4,因此我搜索了具有相同ID的先前兄弟姐妹。组id = 0的g_place id = 3,组id = 1的g_place id = 4,因此我想为组兄弟姐妹+ g_place /的条件g_place / bound

谢谢

0 个答案:

没有答案
相关问题