Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Decoding values From within XSL

I have the following (sample) XML:

<Customers>
  <Name>John Doe</Name>
  <CityCode>NYC</CityCode>
  <Name>Alan Smith</Name>
  <CityCode>LAN</CityCode>
  ...
</Customers>

And want (if possible from within the transforming XSL) to decode the value of <CityCode> from the following XML:

<Cities>
  <CityCode>NYC<Name>New York</Name></CityCode>
  <CityCode>LAN<Name>Los Angeles</Name></CityCode>
</Cities>

I'm at my wits end on how to do this. Thanks


0
JFont
Asked:
JFont
1 Solution
 
chabaudCommented:
First of all, your pb would be easier to resolve if you can reformulate your Xml file, wrapping customers en cities into elements. Note also that City's Name is not a child of CityCode but of City:

<Customers>
 <Customer>
  <Name>John Doe</Name>
  <CityCode>NYC</CityCode>
 </Customer>
 <Customer>
  <Name>Alan Smith</Name>
  <CityCode>LAN</CityCode>
 </Customer>
</Customers>

<Cities>
 <City>
  <CityCode>NYC</CityCode><Name>New York</Name>
 </City>
 <City>
  <CityCode>LAN</CityCode><Name>Los Angeles</Name>
 </City>
</Cities>

Then Xsl file could be simple:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         version="1.0" >

<xsl:output method="html"/>

<xsl:template match="*">
 <xsl:apply-templates/>
</xsl:template>

<xsl:template match="/">
<table border="1">
 <tr><th>Name</th><th>CityCode</th><th>CityName</th></tr>
 <xsl:apply-templates/>
</table>
</xsl:template>

<xsl:template match="Customer">
 <tr>
  <td><xsl:value-of select="Name"/></td>
  <td><xsl:value-of select="CityCode"/></td>
  <td><xsl:value-of select="//City[CityCode=current()/CityCode]/Name"/></td>
 </tr>
</xsl:template>

<xsl:template match="City">
</xsl:template>

</xsl:stylesheet>
0
 
JFontAuthor Commented:
Not only excellent, but very elegant. Thanks.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now