【问题标题】:XSL deleting specific nodes and adding new adding a new nodeXSL 删除特定节点并添加新节点
【发布时间】:2013-02-22 18:47:02
【问题描述】:

我希望删除 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>

我想删除节点&lt;nhm_numerator&gt;&lt;nhm_denominator&gt;并在&lt;varequal&gt;下插入一个新节点(&lt;nhm_blank_value&gt;),同时保留其他两个节点&lt;nhm_blank_name&gt;&lt;nhm_allow_multiples&gt;

新节点的值如下:

<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>

【问题讨论】:

    标签: xml xslt


    【解决方案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>
    

    【讨论】:

    • 太棒了。但是我可以在替换分子节点时添加传递参数值吗?因为此参数值将从 java 程序中填充。
    • @jaykumarark 您的意思是包含整个替换片段还是仅包含分子/分母值的参数?
    • 我的意思是包含片段的参数。参数将从 java 程序接收 mathml 值。我想将参数值放在&lt;varequal&gt; 节点下
    • @jaykumarark 答案取决于您使用的 XSLT 处理器,不同的处理器有不同的方法来传递节点集的参数。 This answer 展示了如何使用 Saxon 进行操作,其他处理器可能会有所不同。一旦你有一个合适的参数值,那么它就是&lt;xsl:template match="nhm_numerator"&gt;&lt;xsl:copy-of select="$theParameter" /&gt;&lt;/xsl:template&gt; 的简单案例。
    • @jaykumarark 确实 - copy-of 直接复制节点,而 value-of 为您提供节点集的字符串值(如果它是一组元素,则意味着所有后代文本节点的连接节点集中的第一个元素)。
    猜你喜欢
    • 2018-12-25
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多