Solved

Lookup tables with variable keys

Posted on 2013-05-24
7
220 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now