2

I have a problem getting data from a node, when I'm using xml:choose and xml:when. I only get the result NaN or the value from the main xml-file, even if.

Part of the XML-file:

<?xml version="1.0" encoding="UTF-8"?>
<Job xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <Invoice>
    <InvoiceLine>
      <LineNo>1</LineNo>
      <QtyInSecondUnit>56</QtyInSecondUnit>
      <Quantity>56</Quantity>
      <CustTaric>
        <StatNo>34011100</StatNo>
        <IssuingCountry>GB</IssuingCountry>
      </CustTaric>
    </InvoiceLine>
    <InvoiceLine>
      <LineNo>2</LineNo>
      <QtyInSecondUnit>22</QtyInSecondUnit>
      <Quantity>0</Quantity>
      <CustTaric>
        <StatNo>44152020</StatNo>
        <IssuingCountry>GB</IssuingCountry>
      </CustTaric>
    </InvoiceLine>
  </Invoice>
</Job>

Part of the XSLT-file:

<?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/">
        <xsl:apply-templates select="Job/Invoice"/>
      </xsl:template>

      <xsl:template match="Job/Invoice/InvoiceLine">
        <xsl:apply-templates select="QtyInSecondUnit"/>
        <xsl:apply-templates select="Quantity"/>
        <xsl:apply-templates select="CustTaric/StatNo"/>
      </xsl:template>

      <xsl:template match="QtyInSecondUnit">
        <xsl:choose>
          <xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
            <xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>
          </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="."/>
          </xsl:otherwise>
        </xsl:choose>   
      </xsl:template>

      <xsl:template match="Quantity">
        <xsl:choose>
          <xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">
            <xsl:value-of select="'0'"/>
          </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="."/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <xsl:template match="CustTaric/StatNo">
        <xsl:value-of select="."/>
      </xsl:template>

Hope there is somebody how can tell me (the noob) what I'm doing wrong here?

1 Answer 1

3

Here's the line producing NaN

<xsl:value-of select="number(translate(Job/Invoice/InvoiceLine/NetMass,',','.')) div 25"/>

There are two problems (one of which is probably where you have over-simplified your XML)

  1. The xpath expression you are using will be relative to the current node you are positioned on. There is no Job element under the current QtyInSecondUnit element
  2. There is no NetMass element in your XML in your question

Assuming NetMass does exist in your actual XML, and is a child of the parent InvoiceLine the expression you want is this

<xsl:value-of select="number(translate(../NetMass,',','.')) div 25"/>

There is also an issue with your xsl:when (possibly)

 <xsl:when test="/Job/Invoice/InvoiceLine/CustTaric/StatNo = '44152020'">

This will test for any CustTaric/StatNo anywhere in the document. Perhaps you only want to test for the one in the current InvoiceLine? If so, do this...

<xsl:when test="../CustTaric/StatNo = '44152020'">

Note, you could rewrite your XSLT to put the logic in template matches, rather than xsl:choose

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Job/Invoice/InvoiceLine">
    <xsl:apply-templates select="QtyInSecondUnit"/>
    <xsl:apply-templates select="Quantity"/>
    <xsl:apply-templates select="CustTaric/StatNo"/>
  </xsl:template>

  <xsl:template match="InvoiceLine[CustTaric/StatNo = '44152020']/QtyInSecondUnit">
    <xsl:value-of select="number(translate(../NetMass,',','.')) div 25"/>
  </xsl:template>

  <xsl:template match="QtyInSecondUnit">
    <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="InvoiceLine[CustTaric/StatNo = '44152020']/Quantity">
    <xsl:value-of select="'0'"/>
  </xsl:template>

  <xsl:template match="Quantity">
    <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="CustTaric/StatNo">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Strictly speaking, the templates that just do <xsl:value-of select="."/> can be removed, as XSLT's built-in templates will do exactly the same thing if there is no matching template in the XSLT.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you :) Works brilliantly!
@ Tim C The noob is stuck again...heh (ashamed). I just wish to sum up Quantity, in all lines where StatNo is not 44152020.
The expression is probably sum(Job/Invoice/InvoiceLine[CustTaric/StatNo != '44152020']/Quantity), but if you can't get it to work, feel free to ask a whole new question. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.