?
Solved

Lookup tables with variable keys

Posted on 2013-05-24
7
Medium Priority
?
231 Views
Last Modified: 2013-05-26
Is it possible to use a lookup table if the key changes? In the example below, a variable is used as the key, but it's not working because XSLT variables are not really variable.

Here is the XSL:
<lookup:NAICS_Classification_Percentage>
	<FacilityNAICS Classification="ccc" Percentage="60">Two Code Inc. Industrial Heartland 123456</FacilityNAICS>
	<FacilityNAICS Classification="ddd" Percentage="40">Two Code Inc. Industrial Heartland 456789</FacilityNAICS>
</lookup:NAICS_Classification_Percentage>

<xsl:variable name="ThisFacilityNAICS" select="concat(/Report/OrganisationNameEn, ' ', /Report/FacilityNameEn, ' ', /Report/Report_Detail/IdentificationProfile/Facility/NAICS_Codes/NAICS/NAICSCodeEn)"/>

<xsl:value-of select="$ThisFacilityNAICS"/>, <xsl:value-of select="document('')//lookup:NAICS_Classification_Percentage/FacilityNAICS[. = $ThisFacilityNAICS]/@Classification"/>, <xsl:value-of select="document('')//lookup:NPRI_GHGIS_ID/CompanyFacility[. = $ThisFacilityNAICS]/@Percentage"/>

Open in new window


Here is the XML:
<Report>
	<OrganisationNameEn>Two Code Inc.</OrganisationNameEn>
	<FacilityNameEn>Industrial Heartland</FacilityNameEn>
	<Report_Detail>
		<IdentificationProfile>
			<Facility>
				<NAICS_Codes>
                    <NAICS>
                        <NAICSCodeEn>123456</NAICSCodeEn>
                    </NAICS>
                    <NAICS>
                        <NAICSCodeEn>456789</NAICSCodeEn>
                    </NAICS>
				</NAICS_Codes>
				</Facility>
				</IdentificationProfile>
				</Report_Detail>
</Report>

Open in new window

0
Comment
Question by:mariita
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39195663
It is absolutely not clear what you want to do
But I assume you are looking for dealing with a combined key?
Wel, combined keys can easily be achieved by concatening the keys
or by having an "and" inside the predicate.

Can you explain what exactly you want to do?
0
 

Author Comment

by:mariita
ID: 39196459
Sorry, the snippet did not have enough context.

I want to loop through the list of NAICS codes in the XML file, and use a composite key (consisting of Organization, Facility, NAICS) to look up the corresponding Classification and Percentage in the lookup table. Because this facility has more than one NAICS code, the $ThisFacilityNAICS variable cannot be set to a specific value.

<lookup:NAICS_Classification_Percentage>
	<FacilityNAICS Classification="ccc" Percentage="60">Two Code Inc. Industrial Heartland 123456</FacilityNAICS>
	<FacilityNAICS Classification="ddd" Percentage="40">Two Code Inc. Industrial Heartland 456789</FacilityNAICS>
</lookup:NAICS_Classification_Percentage>

<xsl:for-each select="/Report/Report_Detail/IdentificationProfile/Facility/NAICS_Codes/NAICS">
	<xsl:variable name="ThisFacilityNAICS" select="concat(/Report/OrganisationNameEn, ' ', /Report/FacilityNameEn, ' ', /Report/Report_Detail/IdentificationProfile/Facility/NAICS_Codes/NAICS/NAICSCodeEn)"/>
	<NAICSCode>
		<NAICSClassification>
			<xsl:value-of select="document('')//lookup:NAICS_Classification_Percentage/FacilityNAICS[. = $ThisFacilityNAICS]/@Classification"/>
		</NAICSClassification>
		<Code>
			<xsl:value-of select="NAICSCodeEn"/>
		</Code>
		<ActivityPercentage>
			<xsl:value-of select="document('')//lookup:NAICS_Classification_Percentage/FacilityNAICS[. = $ThisFacilityNAICS]/@Percentage"/>
		</ActivityPercentage>
	</NAICSCode>
</xsl:for-each>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39196686
just change the lookup

      <FacilityNAICS Classification="ccc" Percentage="60" organisation="Two Code Inc." facility="Industrial Heartland " NAICS="123456"/>

and go for

FacilityNAICS[@organisation = ...][@facility = ...][@NAICS = ...]/@Classification

no need to compound in the lookup, it is better to keep them seperated
0
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 

Author Comment

by:mariita
ID: 39196721
I tried that but it's not working.

<lookup:NAICS_Classification_Percentage>
	<FacilityNAICS Org="Two Code Inc." Facility="Industrial Heartland " NAICS="123456" Classification="ccc" Percentage="60" />
	<FacilityNAICS Org="Two Code Inc." Facility="Industrial Heartland " NAICS="456789" Classification="ddd" Percentage="40" />
</lookup:NAICS_Classification_Percentage>

<xsl:for-each select="/Report/Report_Detail/IdentificationProfile/Facility/NAICS_Codes/NAICS">
	<xsl:value-of select="document('')//lookup:NAICS_Classification_Percentage/FacilityNAICS[@Org = /Report/OrganisationNameEn][@Facility = /Report/FacilityNameEn][@NAICS = NAICSCodeEn]/@Classification"/>, 
	<xsl:value-of select="document('')//lookup:NAICS_Classification_Percentage/FacilityNAICS[@Org = /Report/OrganisationNameEn][@Facility = /Report/FacilityNameEn][@NAICS = NAICSCodeEn]/@Percentage"/>
</xsl:for-each>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39197035
that is a bit tricky (call it the higher science of XSLT :-)

[@Org = /Report/OrganisationNameEn]

because of the document('') the context in teh predicate is the XSLT file, no longer the source XML file. If you need to do that, you need to bind the source to a variable first

<xsl:variable name="source" select="."/>
at the top level of your stylesheet

then you can do this
[@Org = $source/Report/OrganisationNameEn]
0
 

Author Comment

by:mariita
ID: 39197398
How about the third predicate? How would I specify the context?

[@NAICS = NAICSCodeEn]
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 39197405
Hi, I am just giving suggestions, so I have not looked into your stylesheet in detail (will only have time tomorrow, will visit family soon)

As soon as you start looking inside the lookup table, the context will be set to that lookup table, so outside the source XML
In order to get the old context back, you need storage in a variable
You can store the current context at every level

just store away (at the right context) the NAICSCodeEn value as $this-NAICSCodeEn
and use it in the lookup [@NAICS = $this-NAICSCodeEn]
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question