Add two values ​​to xslt, which can sometimes be null

I would like to add values ​​from xml to another using xslt. I am using xml version 1.

<xsl:value-of select="number(/fields/field[@name='value1'])+number(/fields/field[@name='value2'])"/>

How could I do this if value1 or value2 was sometimes empty and would produce NaN?

I know that I can use if and when if value1 or value2 is not empty, but let me say that I can’t check it. How can i solve this?

What I would like to do is if "number (/ fields / field [@ name = 'value2'])" creates NaN, it should be the number 0, then it will work.

Regards Joe

+4
source share
4 answers

if "number (/fields/field [@ name= 'value2'])" NaN it 0

- :

<xsl:decimal-format name="coerce" NaN="0" />
...
<xsl:variable name="a" select="format-number(/fields/field[@name='value1'], '#', 'coerce')"/>
<xsl:variable name="b" select="format-number(/fields/field[@name='value2'], '#', 'coerce')"/>
...
<xsl:value-of select="$a + $b"/>

: , , .

+5

@michael.hor257k - . .

, format-number , xsl:choose, , field (, xsl:choose).

XML

<?xml version="1.0" encoding="utf-8"?>
<fields>
   <pair>
      <field name="value1">2</field>
      <field name="value2">3</field>
   </pair>
   <pair>
      <!--number as strings-->
      <field name="value1">"2"</field>
      <field name="value2">"3"</field>
   </pair>
   <pair>
      <!--empty-->
      <field name="value1"/>
      <field name="value2"/>
   </pair>
</fields>

Stylesheet

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/fields">
      <xsl:for-each select="pair">
         <xsl:variable name="summands">
            <xsl:for-each select="field">
               <field>
                  <xsl:choose>
                     <xsl:when test="number(.) != .">0</xsl:when>
                     <xsl:otherwise>
                        <xsl:value-of select="."/>
                     </xsl:otherwise>
                  </xsl:choose>
               </field>
            </xsl:for-each>
         </xsl:variable>
         <result-a>
            <xsl:value-of select="sum($summands/field)"/>
         </result-a>
      </xsl:for-each>
      <xsl:apply-templates select="//pair" mode="b"/>
   </xsl:template>

   <xsl:decimal-format name="coerce" NaN="0" />

   <xsl:template match="pair" mode="b">
      <xsl:variable name="a" select="format-number(field[1], '#', 'coerce')"/>
      <xsl:variable name="b" select="format-number(field[2], '#', 'coerce')"/>
      <result-b>
         <xsl:value-of select="$a + $b"/>
      </result-b>
   </xsl:template>

</xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8"?>
<result-a>5</result-a>
<result-a>0</result-a>
<result-a>0</result-a>
<result-b>5</result-b>
<result-b>0</result-b>
<result-b>0</result-b>
+2

, , , node , node .

<xsl:value-of select="sum(/fields/field[@name='value1'] | /fields/field[@name='value2'])"/>

node -set , node -set. node, node NaN. , 0.

The limitations are that present but empty nodes will call NaN, as well as nodes containing text.

0
source

You can try xpath boolean (number (// number1)) that return true if the value is a number. You can create a variable that stores a value converter or 0 (if it's NAN) and then make a sum (sotty for my English)

-1
source

Source: https://habr.com/ru/post/1525185/


All Articles