Link to home
Start Free TrialLog in
Avatar of trygves
trygvesFlag for United States of America

asked on

xsl1.0: xml node name based on value in other tag

Hello, in incoming xml I have total amount value; if value is gt 0; the document is invoice; if it is lt 0; it is credit invoice.

The outgoing xml needs different names on tags if invoice or credit note.

I tried like this; but get
  "Expected white space"
when running in Stylus:

<xsl:variable name="MAINHEADER">
      <xsl:choose>
            <xsl:when test="INVOIC/OrderHead/Ord_4182 &lt; 0">
                  <xsl:value-of select="'<CreditNote xmlns=""urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"" xmlns:cac=""urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"" xmlns:cbc=""urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"" xmlns:ccts=""urn:un:unece:uncefact:documentation:2"" xmlns:ext=""urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">'"/>
            </xsl:when>
            <xsl:otherwise>
                  <xsl:value-of select="'<inv:Invoice xmlns:inv=""urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"" xmlns:ext=""urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"" xmlns:cbc=""urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"" xmlns:cac=""urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">'"/>
            </xsl:otherwise>
      </xsl:choose>
      </xsl:variable>

Error message says whitespace should be here (marked bold):
<xsl:value-of select="'<CreditNote

What am I missing?
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

If you want the element generated, you don't need to use value-of (because that would only create a textnode incidentaly starting with "<")
You need to put down the <inv:Invoice in literal nodes

It is pretty hard to do so in a variable
but you can put the elemant name in a variable and use xsl:element
Can you put an example XML and your full XSLT?
Then I could show you how you can do this

Put the choose on the top level (don't use a variable) and do an apply-templates for the other nodes from within the choose

Here is how I would get started with this

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

<xsl:template match="/">
      <xsl:choose>
            <xsl:when test="INVOIC/OrderHead/Ord_4182 &lt; 0">
                  <CreditNote xmlns="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                  <xsl:apply-templates/>
                  </CreditNote>
            </xsl:when>
            <xsl:otherwise>
                  <inv:Invoice xmlns:inv="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                    <xsl:apply-templates/>
                </inv:Invoice>
            </xsl:otherwise>
      </xsl:choose>
      </xsl:variable>
</xsl:template>

<!-- templates for child elements go here -->

</xsl:stylesheet>

Open in new window

Avatar of trygves

ASKER

Thanks, here are my files :)
ehf.xsl
incoming.xml
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial