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>

Open in new window

PugwashXPAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zc2Commented:
The template below forms a comma separated list of not empty elements, it skips the EffectiveDate element and does not put the trailing comma:
<?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 string-length(.)">
			<xsl:if test="position() != 1"><xsl:text>, </xsl:text></xsl:if>
			<xsl:value-of select="."/>
  		</xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Open in new window

0
PugwashXPAuthor Commented:
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'SameAsContactAddrFlag' and 'AddressType'.
0
zc2Commented:
just add

name()!='SameAsContactAddrFlag' and name()!='AddressType' and

into the test attribute
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

PugwashXPAuthor Commented:
Thanks again - almost there. Problem now is that there is a leading comma. See Snippets below.

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

Open in new window

0
zc2Commented:
I created a string variable, it contains all elements names you don't want to be in output.
Then I use text search function contain() to find out is the current element forbidden.
I also use the same condition to find any preceding sibling in order to decide toplace a comma or not to do so.
Also I removed the xsl:if and moved the condition to the xsl:for-each. I gues the position() output you need only for debugging purposes and it will be fine this way.

Hope this solution will be suitable
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" encoding="iso-8859-1"/>
	<xsl:template match="__Address">
		<xsl:variable name="forbidden" select="'(EffectiveDate)(SameAsContactAddrFlag)(AddressType)'"/>
		<xsl:for-each select="*[text() and not(contains($forbidden,concat('(',name(),')')))]">
			<xsl:if test="preceding-sibling::*[text() and not(contains($forbidden,concat('(',name(),')')))]">
				<xsl:text>,</xsl:text>
			</xsl:if>
			<xsl:value-of select="position()"/>
			<xsl:text>,</xsl:text>
			<xsl:value-of select="."/>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PugwashXPAuthor Commented:
Many thanks for all your time
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.