Hi All,

Im new to xsl., In my implementation im converting a language code to a language value by lookup from a xml file.

Input:
<XML>
<Name>shades</Name>
<Languagecode>555</Languagecode>
<XML>

output:
<XML>
<Name>shades</Name>
<Languagecode>hindi</Languagecode>
<XML>

Lookup XML table:(external file)

<Languages>
<Languagecode>555</Languagecode><Language>hindi</Language>
<Languagecode>556</Languagecode><Language>tamil</Language>
<Languagecode>558</Languagecode><Language>telugu</Language>
<Languagecode>559</Languagecode><Language>malayalam</Language>
<Languagecode>557</Languagecode><Language>bengali</Language>
<Languagecode>554</Languagecode><Language>punjabi</Language>
</Languages>

I tried with Key function but still its not working..
thanks in advance..

Regards,
Sathiya

Edited 1 Year Ago by Sathiyaraja: small correction

Hi

Sorry for the delayed reply.

Is there the option to edit the layout of your Lookup file to start with? It could be improved in terms of how you structure it for clarity of querying.

I have achieved the above using XSL 1.0, however your XML file structures are not the best and so I have done it using a modified structure. Let me know if this causes any issues and I will see if it can be adapted.

New input file, this had to be modified to group together the name and language code of each entry, just having one line next to another is not queryable using xsl.

<XML>
  <Data>
    <Name>shades</Name>
    <Languagecode>555</Languagecode>
  </Data>
  <Data>
    <Name>test1</Name>
    <Languagecode>559</Languagecode>
  </Data>
  <Data>
    <Name>test2</Name>
    <Languagecode>557</Languagecode>
  </Data>
</XML>

The lookup file, same as above, needed to group the data.

<Languages>
  <Language code="555">
    <Name>hindi</Name>
  </Language>
  <Language code="556">
    <Name>tamil</Name>
  </Language>
  <Language code="558">
    <Name>telugu</Name>
  </Language>
  <Language code="559">
    <Name>malayalam</Name>
  </Language>
  <Language code="557">
    <Name>bengali</Name>
  </Language>
  <Language code="554">
    <Name>punjabi</Name>
  </Language>
</Languages>

and finally the XSL

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:variable name="lookupDoc" select="document('FULL_FILEPATH_HERE')" />

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

  <xsl:template match="Data">
    <xsl:element name="Data">
      <xsl:element name="Name">
        <xsl:value-of select="./Name/text()"/>
      </xsl:element>
      <xsl:call-template name="MakeSpanForCode">
        <xsl:with-param name="code" select="Languagecode/text()" />
      </xsl:call-template>
    </xsl:element>

  </xsl:template>

  <xsl:template name="MakeSpanForCode">
    <xsl:param name="code" />
    <xsl:element name="Languagecode">
      <xsl:value-of select="$lookupDoc/Languages/Language[@code = $code]/Name/text()" />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

Produces the following

<XML>
  <Data>
    <Name>shades</Name>
    <Languagecode>hindi</Languagecode>
  </Data>
  <Data>
    <Name>test1</Name>
    <Languagecode>malayalam</Languagecode>
  </Data>
  <Data>
    <Name>test2</Name>
    <Languagecode>bengali</Languagecode>
  </Data>
</XML>

Edited 1 Year Ago by Mike Askew

No problem.

Is the restructure of the XML acceptable or did you need to keep it as it originally was? Hopefully it was as otherwise I am not sure it would be possible :)

If it is solved, please mark the question as solved :)

Restucturing will not cause problem, I have resolved using xsl key..
Missed to update the post.
I have attached the example which i tried..

Again thanks Mike for your response..:)

Attachments
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output omit-xml-declaration="yes" indent="yes"/>
	<xsl:strip-space elements="*"/>
	<xsl:key name="currencies-lookup" match="currency" use="code"/>
	<xsl:variable name="states-top" select="document('currencies.xml')/currencies"/>

	<xsl:template match="label">
		<xsl:value-of select="name"/>
		<xsl:apply-templates select="$states-top">
			<xsl:with-param name="curr-label" select="/transdetails/trancurrcode"/>
		</xsl:apply-templates>
	</xsl:template>

	<xsl:template match="/">
		<XML>
			<name>
				<xsl:value-of select="/transdetails/name"/>
			</name>
			<tranamt>
				<xsl:value-of select="/transdetails/tranamt" />
			</tranamt>
			<trancurrcode>
				<xsl:value-of select="/transdetails/trancurrcode"/>
			</trancurrcode>
			<trancurrcodevalue>
				<xsl:template match="currencies">
					<xsl:param name="curr-label"/>
					<xsl:value-of select="key('currencies-lookup',$curr-label)/value"/>
				</xsl:template>
			</trancurrcodevalue>
		</XML>
	</xsl:template>
</xsl:stylesheet>
<transdetails>
<name>sathiya</name>
<tranamt>900</tranamt>
<trancurrcode>111</trancurrcode>
</transdetails>
<currencies>
 <currency><code>111</code><value>QAR</value></currency>
 <currency><code>222</code><value>INR</value></currency>
 <currency><code>333</code><value>USD</value></currency>
 <currency><code>444</code><value>EUR</value></currency>
</currencies>
This question has already been answered. Start a new discussion instead.