Reputation: 2449
I wish to delete few nodes in an xml and add new a node. Let me illustrate with the following 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>
I want to remove the nodes <nhm_numerator>
and <nhm_denominator>
and insert a new node(<nhm_blank_value>
) under <varequal>
while retaining the other two nodes <nhm_blank_name>
<nhm_allow_multiples>
The new node has a value like this:
<nhm_blank_value>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mfrac>
<mn>14</mn>
<mn>25</mn>
</mfrac>
</math>
</nhm_blank_value>
I used the following XSLT to successfully delete the aforementioned nodes. But i couldn't add the new node. Please tell me where I m going wrong
<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>
Upvotes: 1
Views: 306
Reputation: 122414
I would do something like this
<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>
This will pull the right numerator and denominator values from the original XML rather than hard-coding 14 and 25. When run on your sample XML it produces the correct output:
<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>
Upvotes: 1