Reputation: 31
I am using XSL and XPATH to traverse, read and transform a complex XML file where I have multiple nodes with same names but different values. My sample xml file is below -
<core>
<address>
<postalZipCode>90017</postalZipCode>
<updateSource>xxxxxx</updateSource>
<city>LOS ANGELES</city>
<stateProvince>CA</stateProvince>
<type>MAILING</type>
<line1>818 WEST SEVENTH STREET</line1>
</address>
<address>
<postalZipCode>95014</postalZipCode>
<updateSource>xxxxxx</updateSource>
<city>CUPERTINO</city>
<stateProvince>CA</stateProvince>
<type>PRIMARY</type>
<line1>1234 XYZ STREET</line1>
</address>
<memberId>0</memberId>
</core>
When I read the MAILING ADDRESS Line 1 value , the value I am getting is "1234 XYZ STREET" which essentially is the PRIMARY ADDRESS . Here is my xsl file snippet :-
<xsl:template match="core">
<xsl:if test="memberId['0'] and address/type['MAILING']">
<fo:table-row>
<fo:table-cell><fo:block /></fo:table-cell>
<fo:table-cell xsl:use-attribute-sets="data">
<fo:block>Line 1</fo:block>
</fo:table-cell>
<fo:table-cell xsl:use-attribute-sets="data">
<fo:block><xsl:value-of select="address/line1/text()"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
Could the experts here please suggest if there is any other way possible to force XSL to read the correct value of where address is MAILING, instead of reading the first value it finds ???
Upvotes: 0
Views: 292
Reputation: 163595
The code you have written says "if there is an address with type MAILING, then print the value of address/line1 (whether or not this is in any way related to the address with type MAILING)".
In XSLT 1.0, when xsl:value-of
selects more than one node, it displays the value of the first one it finds. In XSLT 2.0 it concatenates the values of all the selected nodes, with a separator that defaults to a single space.
If there's more than one node and you want to select a specific one, then select it with a predicate, e.g. xsl:value-of select="address[@type='MAILING']/line1
.
Upvotes: 1
Reputation: 117140
if there is any other way possible to force XSL to read the correct value of where address is MAILING, instead of reading the first value it finds ???
Your question is rather confusing, because MAILING is the first address in your example - and the snippet you show does not produce the result you say it does.
In any case, to make sure you get data from the MAILING address regardless of its position, change this:
<xsl:value-of select="address/line1/text()"/>
to:
<xsl:value-of select="address[type='MAILING']/line1"/>
To understand how this works, read more about predicates.
Upvotes: 1