I'm trying to transform certain parts of an XML file to another XML file. The source file:
<CUSTOMERS>
<CUSTOMER>
<CUSTOMER_NUMBER>12345678</CUSTOMER_NUMBER>
<CUSTOMER_ADDRESS>
<CUSTOMER_ADDRESS_NAME>John Doe</CUSTOMER_ADDRESS_NAME>
<CUSTOMER_ADDRESS_STREET>Street 1</CUSTOMER_ADDRESS_STREET>
<CUSTOMER_ADDRESS_CITY>Amsterdam</CUSTOMER_ADDRESS_CITY>
</CUSTOMER_ADDRESS>
<CUSTOM_FIELDS>
<CUSTOM_FIELD>
<CUSTOM_FIELD_NAME>Cellphone</CUSTOM_FIELD_NAME>
<CUSTOM_FIELD_VALUE>443209432</CUSTOM_FIELD_VALUE>
</CUSTOM_FIELD>
<CUSTOM_FIELD>
<CUSTOM_FIELD_NAME>Geo</CUSTOM_FIELD_NAME>
<CUSTOM_FIELD_VALUE>323932121,31231233,0</CUSTOM_FIELD_VALUE>
</CUSTOM_FIELD>
</CUSTOM_FIELDS>
</CUSTOMER>
</CUSTOMERS>
The rules:
- Not every
CUSTOMER
hasCUSTOM_FIELDS
, those who don't haveCUSTOM_FIELDS
should not be processed. - When a
CUSTOMER
hasCUSTOM_FIELDS
, the number and order of all theCUSTOM_FIELDS
may vary. - A
CUSTOMER
should only be processed if it has aCUSTOM_FIELD
which name is'Geo'
So in my XSL I've attempted to loop through all CUSTOMERS
, and for each CUSTOMER
loop through the CUSTOM_FIELDS
. When it finds one called 'Geo'
it should output data.
<xsl:for-each select="CUSTOMERS/CUSTOMER">
<xsl:for-each select="CUSTOM_FIELDS/CUSTOM_FIELD">
<xsl:if test=".[CUSTOM_FIELD_NAME='Geo']">
<Placemark>
<name>
<xsl:value-of select="CUSTOMER_NUMBER" />
<xsl:text> </xsl:text>
<xsl:value-of select="CUSTOMER_ADDRESS/CUSTOMER_ADDRESS_NAME" />
</name>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Point>
<coordinates>
<xsl:value-of select="CUSTOM_FIELD_NAME" />
</coordinates>
</Point>
</Placemark>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
But of course at this point I am in CUSTOMERS/CUSTOMER/CUSTOM_FIELDS/CUSTOM_FIELD
so it's not possible to output data from the nodes that are a few levels up.
I've tried setting a variable in the if-block, which should then read after the if-block to see if any data should be returned but I understand variables can only be set once so they can't be used for this goal either.
So my questions are these:
- Do I really need a loop to see if there is a
CUSTOM_FIELD
who'sCUSTOM_FIELD_NAME = 'Geo'
? - If the loop is needed, how can I use a result from inside the loop, outside the loop?
- Or if that is not possible, how can I return values from nodes that are a few levels above the node I'm currently in?