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 に答える