1

xml 内のいくつかのノードを削除し、新しいノードを追加したいと考えています。次の xml で説明します。

<resprocessing>
  <respcondition title="Correct" continue="No">
    <conditionvar>
      <varequal respident="Response_0">
        <nhm_blank_name>Answer:</nhm_blank_name>
        <nhm_numerator>14</nhm_numerator>
        <nhm_denominator>25</nhm_denominator>
        <nhm_allow_multiples>No</nhm_allow_multiples>
      </varequal>
    </conditionvar>
  </respcondition>
</resprocessing>

<nhm_numerator>ノードを削除し、他の 2 つのノードを保持したまま、その 下<nhm_denominator>に新しい node( ) を挿入したい<nhm_blank_value><varequal><nhm_blank_name> <nhm_allow_multiples>

新しいノードには次のような値があります。

<nhm_blank_value>
<math xmlns="http://www.w3.org/1998/Math/MathML">
  <mfrac>
    <mn>14</mn>
    <mn>25</mn>
  </mfrac>
</math>
</nhm_blank_value>

次の XSLT を使用して、前述のノードを正常に削除しました。しかし、新しいノードを追加できませんでした。どこが間違っているのか教えてください

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="mathml">
  <nhm_blank_value>
    <math xmlns="http://www.w3.org/1998/Math/MathML">
      <mfrac>
        <mn>14</mn>
        <mn>25</mn>
      </mfrac>
    </math>
  </nhm_blank_value>
 </xsl:param>
<!-- copy the xml as it is -->   
   <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
   <!-- deleting nodes numerator and denominator -->
   <xsl:template  match="questestinterop/item/resprocessing/respcondition/conditionvar/varequal/nhm_denominator" />
   <xsl:template    match="questestinterop/item/resprocessing/respcondition/conditionvar/varequal/nhm_numerator" />   
   <!-- adding mathml node -->
   <xsl:template match="questestinterop/item/resprocessing/respcondition/conditionvar">
     <xsl:value-of select="varequal">
       <xsl:with-param name="mathml"/>
     </xsl:value-of>
   </xsl:template>   
</xsl:stylesheet>
4

1 に答える 1

1

私はこのようなことをします

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*" />

  <!-- copy the xml as it is -->   
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- delete denominator -->
  <xsl:template  match="nhm_denominator" />

  <!-- replace numerator with mathml fragment -->
  <xsl:template match="nhm_numerator">
    <nhm_blank_value>
      <math xmlns="http://www.w3.org/1998/Math/MathML">
        <mfrac>
          <mn><xsl:value-of select="." /></mn>
          <mn><xsl:value-of select="../nhm_denominator"/></mn>
        </mfrac>
      </math>
    </nhm_blank_value>
  </xsl:template>   

</xsl:stylesheet>

これにより、14 と 25 をハードコーディングするのではなく、元の XML から適切な分子と分母の値が取得されます。サンプル XML で実行すると、正しい出力が生成されます。

<resprocessing>
  <respcondition title="Correct" continue="No">
    <conditionvar>
      <varequal respident="Response_0">
        <nhm_blank_name>Answer:</nhm_blank_name>
        <nhm_blank_value>
          <math xmlns="http://www.w3.org/1998/Math/MathML">
            <mfrac>
              <mn>14</mn>
              <mn>25</mn>
            </mfrac>
          </math>
        </nhm_blank_value>
        <nhm_allow_multiples>No</nhm_allow_multiples>
      </varequal>
    </conditionvar>
  </respcondition>
</resprocessing>
于 2013-02-22T10:41:01.303 に答える