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>并在下面<nhm_denominator>插入一个新节点(<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>

这将从原始 XML 中提取正确的分子和分母值,而不是硬编码 14 和 25。在示例 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 回答