Joining strings/variables in XSLT/Xpath

Hello EE,

I've been looking everywhere but I can't seem to find an answer (maybe I'm not looking hard enough... :()

Anyways, I have an XML document of addresses that I import and extract the variables like the following:

<xsl:variable name="StreetNo" select="Address/StreetNo"/> 
<xsl:variable name="Street" select="Address/Street"/> 
<xsl:variable name="Suburb" select="Address/Suburb"/> 
<xsl:variable name="State" select="Address/State"/> 
<xsl:variable name="Postcode" select="Address/Postcode"/>

Open in new window


I have to join all the variables into one string into a new variable "NewAddress" like the following:

<xsl:variable name="NewAddress" select="$StreetNo + ' ' + $Street + ' ' + $Suburb + ', ' + $State + ' ' + $Postcode"/>

Open in new window


I want it so it'll print a string like "123 Smith Street Sydney, New South Wales 2000".

I'm not getting the right output and it's returning as "NaN". I'm pretty sure the "+" only works with numbers and using a "." doesn't seem to work either.

Hope this makes sense. Can anyone help me with this?

Thank you very much in advance.
LVL 1
mr_stevieAsked:
Who is Participating?
 
mr_stevieAuthor Commented:
Apparently all XML elements passed through to XSL will be string regardless of what kind of data is in each field.

The reason I've been getting NaN is because "+" is an operator used with numerical data and strings can not be added since they're not numeric.

Anyways, I managed to do what I wanted with the following:

 
<xsl:variable name="StreetNo" select="Address/StreetNo"/> 
<xsl:variable name="Street" select="Address/Street"/> 
<xsl:variable name="Suburb" select="Address/Suburb"/> 
<xsl:variable name="State" select="Address/State"/> 
<xsl:variable name="Postcode" select="Address/Postcode"/>
<xsl:variable name="Space" select="' '"/>
<xsl:variable name="Comma" select="', '"/>

<xsl:variable name="NewAddress" select='concat($StreetNo, $Space, $Street, $Comma, $Suburb, $Comma, $State, $Space, $Postcode, $Comma)'/>

Open in new window

0
 
David Johnson, CD, MVPOwnerCommented:
declare everything as a string vice number even address/streetno otherwise you are mixing types and getting the NaN (Not a Number)
0
 
mr_stevieAuthor Commented:
Thanks anyway for your help anyway.
0
 
David Johnson, CD, MVPOwnerCommented:
actually it depends upon your xlst but glad to hear you solved it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.