-4

Here is a snippet of my code. The input is now supposed to include 2 optional fields. So I thought to create 4 if statements, as the URL has to be different if the fields exist or not. The choose/when works, and the first IF statement works. But the last 3 if one or both of the fields are not in the input, blows up. The URL variable(DVURL) does not get populated at all. I'm sure this is easy answer...i hope.

    <?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:func="http://exslt.org/functions" xmlns:date="http://exslt.org/dates-and-times" xmlns:string="http://symphony-cms.com/functions" xmlns:dpfunc="http://www.datapower.com/extensions/functions">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:variable name="datavalueagg" select="test1"/>
        <xsl:variable name="valueagg" select="test2"/>
        <xsl:variable name="variable1" select="Y"/>
        <xsl:choose>
            <xsl:when test="($variable1 = 'Y' or $variable1 = 'N')">
                <xsl:if test="(($datavalueagg != '') and ($valueagg != ''))">
                    <xsl:variable name="DVURL" select="concat('http://server.com/$valueagg/$datavalueagg')"/>
                </xsl:if>
                <xsl:if test="(($datavalueagg != '') and ($valueagg = ''))">
                    <xsl:variable name="DVURL" select="concat('http://server.com/$datavalueagg')"/>
                </xsl:if>
                <xsl:if test="$datavalueagg = '' and $valueagg != ''">
                    <xsl:variable name="DVURL" select="concat('http://server.com/$valueagg')"/>
                </xsl:if>
                <xsl:if test="$datavalueagg = '' and $valueagg = ''">
                    <xsl:variable name="DVURL" select="concat('http://server.com/none')"/>
                </xsl:if>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="DVURL" select="concat('http://server.com/all')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
czah
  • 49
  • 2
  • 7
  • 3
    Present an actual [mcve], not pseudocode. In particular, some of the `select` expressions in your variable definitions are not valid XPath expressions; the details of the real expressions are relevant to the question, and the input XML may be, too. Generally, we want to be able to reproduce your (minimal example) problem. – John Bollinger Jun 20 '17 at 15:28
  • Is edit acceptable? – czah Jun 20 '17 at 16:29
  • 2
    The edit improves the question, but you still do not provide an MCVE. In particular, example input and expected output are needed; follow the link I provided for more information. There are some distinct oddities in the XSL as it is now presented, but I am uncertain whether they are characteristic of your real XML. – John Bollinger Jun 20 '17 at 16:50

1 Answers1

-2

Sorry for poor question, but I used slightly modified code. Working now.

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:func="http://exslt.org/functions" xmlns:date="http://exslt.org/dates-and-times" xmlns:string="http://symphony-cms.com/functions" xmlns:dpfunc="http://www.datapower.com/extensions/functions">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:variable name="datavalueagg" select="test1"/>
        <xsl:variable name="valueagg" select="test2"/>
        <xsl:variable name="variable1" select="Y"/>
        <xsl:choose>
            <xsl:when test="($variable1 = 'Y' or $variable1 = 'N')">
                <xsl:choose>
                    <xsl:when test="(string-length($datavalueagg) != 0 and string-length($valueagg) != 0)">
                        <xsl:variable name="DVURL" select="concat('http://server.com/$valueagg/$datavalueagg')"/>
                    </xsl:when>
                    <xsl:when test="(string-length($datavalueagg) != 0 and string-length($valueagg) != 0)">
                        <xsl:variable name="DVURL" select="concat('http://server.com/$datavalueagg')"/>
                    </xsl:when>
                    <xsl:when test="(string-length($datavalueagg) != 0 and string-length($valueagg) != 0)">
                        <xsl:variable name="DVURL" select="concat('http://server.com/$valueagg')"/>
                    </xsl:when>
                    <xsl:when test="(string-length($datavalueagg) != 0 and string-length($valueagg) != 0)">
                        <xsl:variable name="DVURL" select="concat('http://server.com/none')"/>
                    </xsl:when>
                    <xsl:otherwise/>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="DVURL" select="concat('http://server.com/all')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
czah
  • 49
  • 2
  • 7
  • 1
    Without seeing the input and understanding what you're trying to accomplish, this is not helpful to anyone. – michael.hor257k Jun 20 '17 at 17:13
  • 1
    So it works now that you nested a `` test rather than nested `` ? Was this the only change you made or did you edit anything else somewhere we cannot see? – Will Webb Jun 21 '17 at 09:55
  • Again, without an indication of the inputs and outputs , it is difficult to say what works and what doesnt. That said , the revised code may only show one of the options being chosen based on the input, as the "choose" clause only ever selects a single "when" option. In the previous code , multiple "if " clauses can run if the "if" test results in true. Furthermore , in both code extracts , the variable: "DVURL" is only defined with the "if" or "when" clause, and is not defined outside the main choose element . Again not sure if that is the intended logic . – young chisango Mar 29 '23 at 17:30