xslt concatenation

R

rouble

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:
<location>
<civic-address>
<country>US</country>
<A1>New York</A1>
<A2>King's County</A2>
<A3>New York</A3>
<A4>Manhattan</A4>
<A5>Morningside Heights</A5>
<A6>Broadway</A6>
<PRD>N</PRD>
<POD>SW</POD>
<STS>Street</STS>
<HNO>123</HNO>
<HNS>A</HNS>
<LMK>Low Library</LMK>
<LOC>Room 543</LOC>
<FLR>5</FLR>
<NAM>Joe's Barbershop</NAM>
<PC>10027-0401</PC>
</civic-address>
</location>
<location>
<civic-address>
...
</civic-address>
</location>
<location>
<civic-address>
...
</civic-address>
</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:
<location>
<civic-address>
<country>US</country>
<A1>New York</A1>
<A2>King's County</A2>
<A3>New York</A3>
<A4>Manhattan</A4>
<A5>Morningside Heights</A5>
<street> 123 A N Broadway SW Street </street>
<LMK>Low Library</LMK>
<LOC>Room 543</LOC>
<FLR>5</FLR>
<NAM>Joe's Barbershop</NAM>
<PC>10027-0401</PC>
</civic-address>
</location>
<location>
<civic-address>
...
</civic-address>
</location>
<location>
<civic-address>
...
</civic-address>
</location>

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
 
M

Mukul Gandhi

Here is a stylesheet for this,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:eek:utput method="xml" indent="yes" />

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>

<xsl:template match="civic-address">
<xsl:copy>
<xsl:apply-templates />
<xsl:if test="HNO | HNS | PRD | A6 | POD | STS">
<street><xsl:value-of select="normalize-space(concat(HNO, ' ',
HNS, ' ', PRD, ' ', A6, ' ', POD, ' ', STS))" /></street>
</xsl:if>
</xsl:copy>
</xsl:template>

<xsl:template match="HNO | HNS | PRD | A6 | POD | STS" />

</xsl:stylesheet>

This uses the modified identity transform pattern.

Regards,
Mukul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

selecting a range of nodes 3
data to hash 1
Switching between the signals 4

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top