Search code examples
xml

Are Arithmetic operations evaluated in xml


I have below sample xml,

<Field name="Test" value=5+5></Field>

Here, can the value for "Test" evaluate to 10? Can we perform arthimeric operations inside xml directly. Can anyone please help.


Solution

  • First, it's not XML unless you put quotes round the attribute value:

    <Field name="Test" value="5+5"/>
    

    It's quite common to use XPath for this kind of thing. 5+5 is a valid XPath expression and you can transform your document using XSLT 3.0 to contain

    <Field name="Test" value="10"/>
    

    by doing

    <xsl:template match="Field/@value">
      <xsl:attribute name="value">
        <xsl:evaluate xpath="."/>
      </xsl:attribute>
    </xsl:template>