Search code examples
xmlxsltsoabpel

How to insert a variable into an element's and/or attribute´s value in xslt


I am using xslt in my BPEL to transform my payload into an accepted format for a webservice I am using that features a SQL command. Now I have this element in my payload which value I need to use in my search query.

For example:

<DocumentID>12341241123-124124-124124</DocumentID>

I want to use the value of this element "DocumentID" as a parameter in my SQL query. In the following sense:

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

    <!-- a whole bunch of namespaces -->

           <xsl:template match="/">
              <xsl:for-each select="current()[string-length(DocumentID) >= $STRING_LENGTH]/DocumentID">
                 <xsl:element name="SearchRequest" type="SomeRequestType">
                 <xsl:element name="SearchScope" type="SomeStoreScope" 
                                   objectStore="SomeObjectStore"/>
                 <xsl:element name="SearchSQL">SELECT [Id] FROM Document WHERE
                                               VersionNumber ='{12341241123-124124-124124}'
                                               AND SecondStatement = true</xsl:element>
                 </xsl:element>
              </xsl:for-each>
           </xsl:template>
        </xsl:stylesheet>

However, in the previous example, the "VersionNumber" is hardcoded. I want this to be an parameter overwritten with the actual "DocumentID" element from my payload.

Is there any way in xslt to take the value from an existing payload element and use it in another element's value as a part of it. And how would this be done if the variable should be inserted in an attribute of said element?

Something in this order?

         <xsl:element name="SearchSQL">SELECT [Id] FROM Document WHERE
                                       VersionNumber ='$DocumentID'
                                       AND SecondStatement = true</xsl:element>
         </xsl:element>

Thank you in advance for your help, please feel free to ask for clarification!

Cheers,

Jesper


Solution

  • In XSLT 3.0, set expand-text="yes" and write VersionNumber='{$DocumentID}'.

    In earlier releases write VersionNumber='<xsl:value-of select="$DocumentID"/>'

    Note also that xsl:element does not have a type attribute until version="2.0", and that it doesn't make sense to set the type attribute unless there is an xsl:import-schema declaration.