PugwashXP
asked on
XSL transform to select all non-blank items and also exclude specific nodes
I need a comma separated list excluding the EffectiveDate node and with no trailing comma.
XML Snippet
<AddressList>
<AddressList>
<Address>
<__Address>
<AddressType>CONTACT</AddressType>
XSL snippet
<xsl:for-each select="*[.!='']">
<xsl:choose>
<xsl:when test="contains(local-name(), 'EffectiveDate')">
<!-- do nothing-->
</xsl:when>
<xsl:when test="contains(local-name(),'SameAsContactAddrFlag')">
<!-- do nothing-->
</xsl:when>
<xsl:when test="contains(local-name(),'AddressType')">
<!-- do nothing-->
</xsl:when>
<xsl:otherwise>
<xsl:if test="string()">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<SameAsContactAddrFlag/>
<BldgName/>
<SubBldgName/>
<BldgNumber>1</BldgNumber>
<Street1>Grange Close North</Street1>
<Street2/>
<Locality1>Henleaze</Locality1>
<Locality2/>
<Town>Bristol</Town>
<County/>
<PostCode>BS97BY</PostCode>
<OrgName/>
<DeptName/>
<PoBox/>
<EffectiveDate>2006-10-26 11:45:50</EffectiveDate>
</__Address>
</__Address>
</Address>
</AddressList>
</AddressList>
ASKER
Many thanks for that which does exactly what's asked.
I'm afraid I omitted oine of the requirements which is also to skip the nodes'SameAsContactAddrFla g' and 'AddressType'.
I'm afraid I omitted oine of the requirements which is also to skip the nodes'SameAsContactAddrFla
just add
name()!='SameAsContactAddr Flag' and name()!='AddressType' and
into the test attribute
name()!='SameAsContactAddr
into the test attribute
ASKER
Thanks again - almost there. Problem now is that there is a leading comma. See Snippets below.
Cheers
Cheers
XSL
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="__Address">
<xsl:for-each select="*">
<xsl:if test="name()!='EffectiveDate' and name()!='SameAsContactAddrFlag' and name()!='AddressType' and
string-length(.)">
<xsl:if test="position() != 1">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:value-of select="position()"/><xsl:text>, </xsl:text>
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML
<?xml version="1.0" encoding="UTF-8"?>
<__Address>
<AddressType>CONTACT</AddressType>
<BldgName/>
<SubBldgName/>
<BldgNumber>47</BldgNumber>
<Street1>RRRRRR</Street1>
<Street2>ssssss</Street2>
<Locality1/>
<Locality2/>
<Town>PEACEHAVEN</Town>
<County/>
<PostCode>BN10 7PE</PostCode>
<PoBox/>
</__Address>
OUTPUT
<?xml version="1.0" encoding="UTF-8"?>, 4, 47, 5, RRRRRR, 6, ssssss, 9, PEACEHAVEN, 11, BN10 7PE
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks for all your time
Open in new window