Reputation: 1095
I have this xml data
<XMLCreators>
<row>
<RateNumber>1</RateNumber>
<RateLetter>Null</RateLetter>
<AssessmentStreet>Abesinia Passage</AssessmentStreet>
<RateAccomDesc>Dwelling (Part Of)</RateAccomDesc>
</row>
<row>
<RateNumber>1a</RateNumber>
<RateLetter>Null</RateLetter>
<AssessmentStreet>Arena's Palace Lane</AssessmentStreet>
<RateAccomDesc>Edmund's Home</RateAccomDesc>
</row>
</XMLCreators>
I changed its output element with this xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/">
<Address> <xsl:apply-templates/> </Address>
</xsl:template>
<xsl:template match="/XMLCreators/row">
<Rowinfo> <xsl:apply-templates/> </Rowinfo>
</xsl:template>
<xsl:template match="/XMLCreators/row/RateNumber">
<Locator> <xsl:apply-templates/> </Locator>
</xsl:template>
<xsl:template match="/XMLCreators/row/AssessmentStreet">
<thoroughfare> <xsl:apply-templates/> </thoroughfare>
</xsl:template>
<xsl:template match="/XMLCreators/row/RateAccomDesc">
<LocatorName> <xsl:apply-templates/> </LocatorName>
</xsl:template>
</xsl:stylesheet>
and produced this
<?xml version="1.0" encoding="utf-8"?>
<Address>
<Rowinfo>
<Locator>1</Locator>
Null
<thoroughfare>Abesinia Passage</thoroughfare>
<LocatorName>Dwelling (Part Of)</LocatorName>
</Rowinfo>
<Rowinfo>
<Locator>1a</Locator>
Null
<thoroughfare>Arena's Palace Lane</thoroughfare>
<LocatorName>Edmund's Home</LocatorName>
</Rowinfo>
</Address>
I want to add a new element to the rowinfo (locatordesignator) and populate it with the concatenation of the values of (rateletter and RateAccomDesc).
Please, I need help with this.
Upvotes: 1
Views: 96
Reputation: 243459
Just add this template:
<xsl:template match="RateLetter">
<LocatorDesignator>
<xsl:value-of select="concat(., ', ', ../RateAccomDesc)"/>
</LocatorDesignator>
</xsl:template>
The complete transformation now becomes:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<Address> <xsl:apply-templates/> </Address>
</xsl:template>
<xsl:template match="row">
<Rowinfo> <xsl:apply-templates/> </Rowinfo>
</xsl:template>
<xsl:template match="RateNumber">
<Locator> <xsl:apply-templates/> </Locator>
</xsl:template>
<xsl:template match="AssessmentStreet">
<thoroughfare> <xsl:apply-templates/> </thoroughfare>
</xsl:template>
<xsl:template match="RateAccomDesc">
<LocatorName> <xsl:apply-templates/> </LocatorName>
</xsl:template>
<xsl:template match="RateLetter">
<LocatorDesignator>
<xsl:value-of select="concat(., ', ', ../RateAccomDesc)"/>
</LocatorDesignator>
</xsl:template>
</xsl:stylesheet>
and when it is applied on the provided XML document:
<XMLCreators>
<row>
<RateNumber>1</RateNumber>
<RateLetter>Null</RateLetter>
<AssessmentStreet>Abesinia Passage</AssessmentStreet>
<RateAccomDesc>Dwelling (Part Of)</RateAccomDesc>
</row>
<row>
<RateNumber>1a</RateNumber>
<RateLetter>Null</RateLetter>
<AssessmentStreet>Arena's Palace Lane</AssessmentStreet>
<RateAccomDesc>Edmund's Home</RateAccomDesc>
</row>
</XMLCreators>
the wanted, correct result is produced:
<Address>
<Rowinfo>
<Locator>1</Locator>
<LocatorDesignator>Null, Dwelling (Part Of)</LocatorDesignator>
<thoroughfare>Abesinia Passage</thoroughfare>
<LocatorName>Dwelling (Part Of)</LocatorName>
</Rowinfo>
<Rowinfo>
<Locator>1a</Locator>
<LocatorDesignator>Null, Edmund's Home</LocatorDesignator>
<thoroughfare>Arena's Palace Lane</thoroughfare>
<LocatorName>Edmund's Home</LocatorName>
</Rowinfo>
</Address>
Explanation:
Proper use of templates and the Standard XPath concat()
function.
Upvotes: 1
Reputation: 220762
Just add the additional information to one of your templates:
<xsl:template match="/XMLCreators/row">
<Rowinfo>
<locatordesignator>
<xsl:value-of select="RateLetter"/>
<xsl:text> </xsl:text>
<xsl:value-of select="RateAccomDesc"/>
</locatordesignator>
<xsl:apply-templates/>
</Rowinfo>
</xsl:template>
Upvotes: 0