Reputation: 21
I've looked through a number of Q&A's here, but I'm still struggling how to start/get it done (I'm not fluent in xslt/xml). I've an XSL creating an XML feed. Now in addition, I've been given an XML file, in which I need to lookup a brand, and return the country and ID.
The content of the brand_country.xml file:
<Make>
<man_id>22</man_id>
<man_name>Bentley</man_name>
<man_country>Britain</man_country>
<_type_>car</_type_>
</Make>
<Make>
<man_id>23</man_id>
<man_name>Benz</man_name>
<man_country>Germany</man_country>
<_type_>car</_type_>
</Make>
<Make>
<man_id>24</man_id>
<man_name>Berkley</man_name>
<man_country>Britain</man_country>
<_type_>car</_type_>
</Make>
<Make>
<man_id>25</man_id>
<man_name>Bitter</man_name>
<man_country>Germany</man_country>
<_type_>car</_type_>
</Make>
<Make>
<man_id>28</man_id>
<man_name>BMW</man_name>
<man_country>Germany</man_country>
<_type_>car</_type_>
</Make>
Now in my xsl I've
<xsl:for-each select="entries/entry">
<root>
<channel>
<ad>
<category_id>1</category_id>
<ad_id><xsl:value-of select="id" /></ad_id>
<locale>en</locale>
<country>n/a</country>
<make_id><xsl:value-of select="fields/field_make/data" /> </make_id>
<year><xsl:value-of select="fields/field_year/data" /></year>
<handling><xsl:value-of select="fields/field_lhdrhd/data" /></handling>
<heading><xsl:value-of select="fields/field_make/data" /> <xsl:text> </xsl:text> <xsl:value-of select="name" /></heading>
<reg_no> </reg_no>
<chassis_no><xsl:value-of select="fields/field_chassis_nr/data" /></chassis_no>
<engine_no> </engine_no>
<price_type>
<xsl:choose>
<xsl:when test="string-length( fields/field_price_poa/data )">
<xsl:text>POA</xsl:text>
</xsl:when>
<xsl:otherwise>Asking Pricefix</xsl:otherwise>
</xsl:choose>
</price_type>
<price>
<xsl:choose>
<xsl:when test="string-length( fields/field_price_poa/data )">
<xsl:text> </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="fields/field_price/data" />
</xsl:otherwise>
</xsl:choose>
</price>
<currency_id>
<xsl:choose>
<xsl:when test="fields/field_currency/data = 'GBP'">
<xsl:text>20</xsl:text>
</xsl:when>
<xsl:when test="fields/field_currency/data = 'USD'">
<xsl:text>10</xsl:text>
</xsl:when>
</xsl:choose>
</currency_id>
</ad>
</channel>
</root>
</xsl:for-each>
This is what I need for the current feed.
Now, I would need to use the content of…
<xsl:value-of select="fields/field_make/data" />
…to lookup in the brand_country.xml file, and find:
<make_id>... </make_id>
<country> ... </country>
this is what I have so far:
(cut....)
<xsl:key name="mancountry" match="man_country" use="../man_name"/>
<xsl:key name="manid" match="man_id" use="../man_name"/>
<xsl:template match="/section|/category|/entry_details">
<xsl:for-each select="entries/entry">
<root>
<channel>
<ad>
<category_id>1</category_id>
<ad_id><xsl:value-of select="id" /></ad_id>
<locale>en</locale>
<xsl:variable name="inputmake" select="fields/field_make/data"/>
<country>
<xsl:for-each select="document('http://www.xxx.yyy/dev/feed_data/brand_country.xml')">
<xsl:variable name="value" select="key('mancountry',$inputmake)"/>
<xsl:choose>
<xsl:when test="$value">
<xsl:value-of select="$value"/>
</xsl:when>
<xsl:otherwise>world</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</country>
(cut....)
I appreciate any help and tips.
Upvotes: 2
Views: 2649
Reputation: 167516
With XSLT 2.0 put the following as top-level code inside of xsl:stylesheet
:
<xsl:param name="lk" select="'brand_country.xml'"/>
<xsl:variable name="lk-doc" select="doc($lk)"/>
<xsl:key name="brand" match="Make" use="man_name"/>
Now to look up values simply use
<xsl:variable name="make" select="key('brand', fields/field_make/data, $lk-doc)"/>
respectively
<country><xsl:value-of select="$make/man_country"/></country>
With XSLT 1.0 you can use
<xsl:param name="lk" select="'brand_country.xml'"/>
<xsl:variable name="lk-doc" select="document($lk)"/>
<xsl:key name="brand" match="Make" use="man_name"/>
then
<xsl:variable name="this" select="."/>
<xsl:for-each select="$lk-doc">
<xsl:variable name="make" select="key('brand', $this/field_make/data)"/>
<country><xsl:value-of select="$make/man_country"/></country>
</xsl:for-each>
Upvotes: 2