0

I am trying to loop an array and concatenate the non null values to a single variable. Needless to say I am new to XSLT. Please see the code that I wrote and let me know the best way to do this.

\ is to be appended at the end and also I have to add a separator * between the values. If * and \ are coming together, then only \ should come and we can omit the *. I believe the logic is correct. The syntax is the issue.

Thanks, Anoop

<xsl:variable name="secondsegmentarray">
    <item>Data/Attribute1</item>
    <item>Data/Attribute2</item>
    <item>Data/Attribute3</item>
    <item>Data/Attribute4</item>
    <item>Data/Attribute5</item>
    <item>Data/Attribute6</item>
    <item>Data/Attribute7</item>
</xsl:variable>
<xsl:variable name="secondsegment">
    <xsl:value-of select="'\'">
    <xsl:for-each select="secondsegmentarray/item">
        <xsl:choose>
         <xsl:when test="$secondsegmentarray.item = '' and secondsegment='\'"/>
         </xsl:when>
         <xsl:when test="$secondsegmentarray.item = not('') and secondsegment='\'"/>
           <xsl:value-of select="concat($secondsegmentarray.item,secondsegment)"/>
         </xsl:when>
         <xsl:when test="$secondsegmentarray.item = not('') and secondsegment!='\'"/>
           <xsl:value-of select="concat($secondsegmentarray.item,'*',secondsegment)"/>
         </xsl:when>
         <xsl:when test="$secondsegmentarray.item = '' and secondsegment!='\'"/>
           <xsl:value-of select="concat('*',secondsegment)"/>
         </xsl:when>
        </xsl:choose>
    </xsl:for-each>
<xsl:variable/>
3
  • can you give the expected output for something? And compare with the current output/list any errors you get? Commented Apr 26, 2019 at 12:26
  • sure @RAB. Meantime can you tell me if the syntax is right? Commented Apr 26, 2019 at 12:38
  • If this is an XSLT 1.0 stylesheet, then your $secondsegmentarray will be of Result Tree Fragment type (the worst decision about the language specification). And then, of course, you cannot iterate over a Result Tree Fragment (from the spec): "An operation is permitted on a result tree fragment only if that operation would be permitted on a string [..]. In particular, it is not permitted to use the /, //, and [] operators on result tree fragments". Commented Apr 26, 2019 at 12:50

1 Answer 1

1

Not really an answer, but too long to fit in a comment. You have numerous syntax issues, starting with:

<xsl:for-each select="secondsegmentarray/item">

which is:

  • missing a $ character to identify a variable, and:
  • trying to process a result-tree-fragment without converting it to a node-set first.

The correct syntax would be:

<xsl:for-each select="exsl:node-set($secondsegmentarray)/item">

afer declaring:

xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"

in the header.

Next, within the xsl:for-each instruction you are in the context of item. This means that any tests related to the current item need to reference the current node, for example:

 <xsl:when test=". = ''">

Your attempted use of $secondsegmentarray.item is meaningless in XPath/XSLT.

In addition, you are referencing the secondsegment variable (again, without the required $ prefix) in the definition of the variable itself. This, of course, cannot work.

There may be more, but these stand out.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.