Connecting Tech Pros Worldwide Forums | Help | Site Map

xslt concatenation

Newbie
 
Join Date: Jan 2009
Posts: 1
#1: Jan 14 '09
Hi All,

I am just picking up xslt and I was wondering if I could get some
pointers. I have an xml body that looks like this:
Expand|Select|Wrap|Line Numbers
  1.    <location>
  2.        <civic-address>
  3.           <country>US</country>
  4.           <A1>New York</A1>
  5.           <A2>King's County</A2>
  6.           <A3>New York</A3>
  7.           <A4>Manhattan</A4>
  8.           <A5>Morningside Heights</A5>
  9.           <A6>Broadway</A6>
  10.           <PRD>N</PRD>
  11.           <POD>SW</POD>
  12.           <STS>Street</STS>
  13.           <HNO>123</HNO>
  14.           <HNS>A</HNS>
  15.           <LMK>Low Library</LMK>
  16.           <LOC>Room 543</LOC>
  17.           <FLR>5</FLR>
  18.           <NAM>Joe's Barbershop</NAM>
  19.           <PC>10027-0401</PC>
  20.        </civic-address>
  21.     </location>
  22.    <location>
  23.        <civic-address>
  24.            ...
  25.        </civic-address>
  26.     </location>
  27.    <location>
  28.        <civic-address>
  29.            ...
  30.        </civic-address>
  31.     </location>
I need to translate this (using xslt) to create a new element called
'street' that has the values of HNO HNS PRD A6 POD STS from one civic-
address. So the output should look like:
Expand|Select|Wrap|Line Numbers
  1.    <location>
  2.        <civic-address>
  3.           <country>US</country>
  4.           <A1>New York</A1>
  5.           <A2>King's County</A2>
  6.           <A3>New York</A3>
  7.           <A4>Manhattan</A4>
  8.           <A5>Morningside Heights</A5>
  9.           <street> 123 A N Broadway SW Street </street>
  10.           <LMK>Low Library</LMK>
  11.           <LOC>Room 543</LOC>
  12.           <FLR>5</FLR>
  13.           <NAM>Joe's Barbershop</NAM>
  14.           <PC>10027-0401</PC>
  15.        </civic-address>
  16.    </location>
  17.    <location>
  18.        <civic-address>
  19.            ...
  20.        </civic-address>
  21.     </location>
  22.    <location>
  23.        <civic-address>
  24.            ...
  25.        </civic-address>
  26.     </location>
  27.  
If any of HNO HNS PRD A6 POD STS are missing, they should just be
ignored. If all of them are missing, then a 'street' element should
not be created.

I am looking for tips on creating an efficient xslt for this.

TIA,
rouble

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,658
#2: Jan 15 '09

re: xslt concatenation


Quote:

Originally Posted by rouble View Post

I am looking for tips on creating an efficient xslt for this.

well, if you haven't done yet, I recommend reading a tutorial (www.w3schools.com/xslt). then you should try to put the creation of the <street> tag in a separate template. once that is running you need to put it in the copy-procedure (you'll find some copy templates in this forum).

if you're running into problems, just ask us

regards
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#3: Jan 15 '09

re: xslt concatenation


Quote:
If all of them are missing
Do you mean the elements themselves are missing, or there are empty elements? The solution can be quite different with each.

The solution with empty elements is much easier, and looks like:
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="A6">
  2.   <xsl:variable name="street">
  3.     <xsl:value-of select="."/>
  4.     <xsl:value-of select="following-sibling::PRD"/>
  5.     <xsl:value-of select="following-sibling::POD"/>
  6.     <xsl:value-of select="following-sibling::STS"/>
  7.     <xsl:value-of select="following-sibling::HNO"/>
  8.     <xsl:value-of select="following-sibling::HNS"/>
  9.   </xsl:variable>
  10.   <xsl:if test="$street != ''">
  11.     <street><xsl:value-of select="$street"/></street>
  12.   </xsl:if>
  13. </xsl:template>
  14. <xsl:template match="PRD|POD|STS|HNO|HNS"/>
  15.  
The other solution requires you to make changes at the civic-address template level, and is much more painful.
Reply