1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <SEGMEM>
      <FIELD>
        <Entry type="Field">
          <MemoryVariable>x</MemoryVariable>
          <Variable>x(v9)</Variable>
          <offset>
            <lowerBound>0</lowerBound>
            <upperBound>31</upperBound>
          </offset>
        </Entry>
        <Entry type="Field">
          <MemoryVariable>LE</MemoryVariable>
          <Variable>LE(v10)</Variable>
          <offset>
            <lowerBound>0</lowerBound>
            <upperBound>0</upperBound>
          </offset>
        </Entry>
     </FIELD>
    <CONGRUENCES>
        <Entry type="congruence">
          <Variable>v6</Variable>
          <Value>
            <multiplier>0</multiplier>
            <offset>1</offset>
          </Value>
        </Entry>
        <Entry type="congruence">
          <Variable>x(v9)</Variable>
          <Value>
            <multiplier>1</multiplier>
            <offset>0</offset>
          </Value>
        </Entry>
      </CONGRUENCES>
    </SEGMEM> 

我有上面的 XML。<Entry type="Field">我需要搜索常见的变量元素 <Entry type="congruence">。变量只会显示一次。

输出应如下所示:

<table border="1">
      <tr bgcolor="#9acd32">
        <th>MemoryVariable</th>
        <th>Variable</th>
        <th>lowerbound/upperbound</th>
        <th>multiplier/offset</th>
      </tr>
     <tr>
         <td>x</td>      <!-- memory variable -->
         <td>x(v9)</td>  <!-- x(v9) is the in file and congruence entry -->
         <td>[0,31]</td> <!-- lowerbound and upperbound -->
         <td>[1,0]</td>  <!-- <multiplier>1</multiplier> <offset>0</offset> -->
       </tr>
      <tr>
         <td>LE</td>     <!-- memory variable again -->
         <td>LE(v10)</td><!-- LE(v10) is the in file and not in congruence entry -->
         <td>[0,0]</td>  <!-- lowerbound and upperbound -->
         <td>null</td>   <!-- <multiplier>null</multiplier> <offset>null</offset> -->
      </tr>
      <tr>
         <td>null</td>   <!-- memory variable again -->
         <td>v6</td>     <!-- v6 is the not in file but in congruence entry -->
         <td>null</td>   <!-- lowerbound and upperbound -->
         <td>[0,1]</td>  <!-- <multiplier>0</multiplier> <offset>1</offset> -->
    </tr>
</table>   

哪位高手可以给出解决方案?我解决不了。

4

1 回答 1

1
<!-- index Entries by their Values -->
<xsl:key name="fields" match="FIELD/Entry" use="Variable" />
<xsl:key name="congruence" match="CONGRUENCES/Entry" use="Variable" />

<xsl:template match="SEGMEM">
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>MemoryVariable</th>
      <th>Variable</th>
      <th>Bounds</th>
      <th>Congruence</th>
    </tr>
    <xsl:apply-templates select=".//Entry" />
  </table>
</xsl:template>

<xsl:template match="Entry[parent::FIELD]">
  <!-- find matching conguruence entry -->
  <xsl:variable name="c" select="key('congruence', Variable)" />
  <tr>
    <td><xsl:value-of select="MemoryVariable" /></td>
    <td><xsl:value-of select="Variable" /></td>
    <td><xsl:apply-templates select="offset" mode="bracket" /></td>
    <td>
      <xsl:choose>
        <xsl:when test="$c"><xsl:apply-templates select="$c/Value" mode="bracket" /></xsl:when>
        <xsl:otherwise>null</xsl:otherwise>
      </xsl:choose>
    </td>
  </tr>
</xsl:template>

<xsl:template match="Entry[parent::CONGRUENCES]">
  <!-- only output this if there is no matching field entry -->
  <xsl:if test="not(key('fields', Variable))">
    <tr>
      <td>null</td>
      <td><xsl:value-of select="Variable" /></td>
      <td>null</td>
      <td><xsl:apply-templates select="Value" mode="bracket" /></td>
    </tr>
  </xsl:if>
</xsl:template>

<xsl:template match="*" mode="bracket">
  <xsl:value-of select="concat('[', *[1], ',', *[2], ']')" />
</xsl:template>

给你

<table border="1">
  <tr bgcolor="#9acd32">
    <th>MemoryVariable</th>
    <th>Variable</th>
    <th>Bounds</th>
    <th>Congruence</th>
  </tr>
  <tr>
    <td>x</td>
    <td>x(v9)</td>
    <td>[0,31]</td>
    <td>[1,0]</td>
  </tr>
  <tr>
    <td>LE</td>
    <td>LE(v10)</td>
    <td>[0,0]</td>
    <td>null</td>
  </tr>
  <tr>
    <td>null</td>
    <td>v6</td>
    <td>null</td>
    <td>[0,1]</td>
  </tr>
</table>

http://www.xmlplayground.com/P9YpKF

于 2013-11-13T16:30:49.293 回答