xml - XSLT: concatenate two field values if not null -


i need modify , existing xml document concatenate field values. i'm not sure how this, i'm unable keep variables in scope.

basically if there value present firstname , lastname, should output firstname+lastname. if either or both values firstname or lastname empty, should not output fullname. note existing document , unable modify existing code above line #34.

here have far:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"     xmlns:samlp="urn:oasis:names:tc:saml:2.0:protocol"     xmlns:ds="http://www.w3.org/2000/09/xmldsig#">   <xsl:template match="/">     <authenticator>         <userinfo>             <xsl:for-each select="/samlp:response/*[local-name() = 'assertion']/*[local-name() = 'attributestatement']/*">                  <xsl:if test="@name='first_name'">                     <xsl:variable name="firstname" select="*[local-name() = 'attributevalue']" />                     <xsl:element name="field">                           <xsl:attribute name="name">firstname</xsl:attribute>                             <xsl:attribute name="value">                                 <xsl:for-each select="*[local-name() = 'attributevalue']">                                   <xsl:value-of select="normalize-space(current())"/>                              </xsl:for-each>                          </xsl:attribute>                      </xsl:element>                  </xsl:if>                  <xsl:if test="@name='last_name'">                     <xsl:variable name="lastname" select="*[local-name() = 'attributevalue']" />                     <xsl:element name="field">                           <xsl:attribute name="name">lastname</xsl:attribute>                              <xsl:attribute name="value">                                 <xsl:for-each select="*[local-name() = 'attributevalue']">                                   <xsl:value-of select="normalize-space(current())"/>                              </xsl:for-each>                          </xsl:attribute>                      </xsl:element>                  </xsl:if>                  <!-- unable modify above line-->                 <!-- if firstname & lastname not null, concatinate firstname + lastname-->                 <xsl:if test="not($firstname = '') or not($lastname = '')">                     <xsl:element name="field">                         <xsl:attribute name="name">fullname</xsl:attribute>                         <xsl:attribute name="value">                             <xsl:value-of select="concat($firstname,'+',$lastname)" />                         </xsl:attribute>                     </xsl:element>                 </xsl:if>              </xsl:for-each>          </userinfo>     </authenticator>   </xsl:template> </xsl:stylesheet> 

simplified xml file below:

<samlp:response id="_f9daea33-e32a-4dde-beb4-d5227690b1a3" version="2.0" issueinstant="2015-07-30t15:06:58.874z" destination="https://domain.net/login/pauthentication.aspx?configset=saml" xmlns:samlp="urn:oasis:names:tc:saml:2.0:protocol"> <saml:issuer xmlns:saml="urn:oasis:names:tc:saml:2.0:assertion">urn:jh:identityprovider</saml:issuer> <samlp:status>     <samlp:statuscode value="urn:oasis:names:tc:saml:2.0:status:success" /> </samlp:status> <saml:assertion version="2.0" id="_b54ca592-4401-4107-a426-281918091842" issueinstant="2015-07-30t15:06:58.898z" xmlns:saml="urn:oasis:names:tc:saml:2.0:assertion">     <saml:attributestatement>         <saml:attribute name="firstname" nameformat="urn:oasis:names:tc:saml:2.0:attrname-format:unspecified">             <saml:attributevalue>john</saml:attributevalue>         </saml:attribute>     </saml:attributestatement>             <saml:attributestatement>         <saml:attribute name="lastname" nameformat="urn:oasis:names:tc:saml:2.0:attrname-format:unspecified">             <saml:attributevalue>doe</saml:attributevalue>         </saml:attribute>     </saml:attributestatement> </saml:assertion> 

the example rather confusing - it's not clear "record" , how many records can have (judging xslt, one?).

see if can use starting point. assumes each saml:assertion record, , outputs fullname field if record has both firstname , lastname.

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:samlp="urn:oasis:names:tc:saml:2.0:protocol" xmlns:saml="urn:oasis:names:tc:saml:2.0:assertion" exclude-result-prefixes="samlp saml"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <xsl:template match="/samlp:response">     <authenticator>         <xsl:for-each select="saml:assertion">             <userinfo>                 <xsl:variable name="firstname" select="saml:attributestatement/saml:attribute[@name='firstname']/saml:attributevalue"/>                 <xsl:variable name="lastname" select="saml:attributestatement/saml:attribute[@name='lastname']/saml:attributevalue"/>                 <xsl:if test="$firstname , $lastname">                     <field name="fullname" value="{concat($firstname, ' ', $lastname)}"/>                   </xsl:if>             </userinfo>                  </xsl:for-each>     </authenticator>         </xsl:template>  </xsl:stylesheet> 

test input

<samlp:response xmlns:samlp="urn:oasis:names:tc:saml:2.0:protocol">    <saml:assertion xmlns:saml="urn:oasis:names:tc:saml:2.0:assertion">       <saml:attributestatement>          <saml:attribute name="firstname">             <saml:attributevalue>john</saml:attributevalue>          </saml:attribute>       </saml:attributestatement>       <saml:attributestatement>          <saml:attribute name="lastname">             <saml:attributevalue>doe</saml:attributevalue>          </saml:attribute>       </saml:attributestatement>    </saml:assertion>    <saml:assertion xmlns:saml="urn:oasis:names:tc:saml:2.0:assertion">       <saml:attributestatement>          <saml:attribute name="firstname">             <saml:attributevalue>madonna</saml:attributevalue>          </saml:attribute>       </saml:attributestatement>    </saml:assertion> </samlp:response> 

result

<?xml version="1.0" encoding="utf-8"?> <authenticator>   <userinfo>     <field name="fullname" value="john doe"/>   </userinfo>   <userinfo/> </authenticator> 

note use of prefixes call out elements in source xml.


Comments