如何从XSLT 2.0中的国家/地区名称获取ISO国家/地区代码

时间:2015-08-10 05:03:24

标签: xslt xslt-1.0 xslt-2.0

是否有任何内置函数或方法来获取XSLT 2.0中的ISO国家/地区代码?我在请求中获得了一个国家/地区名称,我必须在传出请求中传递国家/地区代码。一种方法是将每个国家/地区名称映射到其各自的国家/地区代码,但这需要花费大量时间,因此有任何捷径可以做到吗?

3 个答案:

答案 0 :(得分:1)

如果你有像

这样的数据
<codes>
  <country code="gb" name="United Kingdom"/>
  <country code="de" name="Germany"/>
  ...
</codes>

然后你通常定义一个键

<xsl:key name="ccode" match="country" use="@code"/>

然后使用

获取国家/地区的名称
key('ccode', 'be', $country-codes)/@name

返回“比利时”

其中$ country-codes是包含此数据的文档。

答案 1 :(得分:0)

  

是否有任何内置函数或方法来获取XSLT中的ISO国家/地区代码   2.0

不,当然不是。使用查找表。

  

一种方法是将每个国家/地区名称映射到各自的国家/地区代码,   但这需要花费很多时间,所以有任何捷径可做   是什么?

它已经完成,例如: https://en.wikipedia.org/wiki/ISO_3166-1#Officially_assigned_code_elements

答案 2 :(得分:0)

以防其他人在尝试解决与原始海报相同的问题时发现此问题。我能够使用@ Michael-Kay

提供的所有详细信息来解决这个问题

源XML

<?xml version="1.0" encoding="utf-8"?>
<Document>
    <SalesOrder>        
        <Country>DK</Country>       
    </SalesOrder>
</Document>

XSLT /样式表

<xsl:transform  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:variable name="countries">
        <countries>
            <country name="Cuba" alpha-2="CU" alpha-3="CUB" country-code="192" iso_3166-2="ISO 3166-2:CU" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Caribbean" region-code="019" sub-region-code="419" intermediate-region-code="029"/>
            <country name="Curaçao" alpha-2="CW" alpha-3="CUW" country-code="531" iso_3166-2="ISO 3166-2:CW" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Caribbean" region-code="019" sub-region-code="419" intermediate-region-code="029"/>
            <country name="Cyprus" alpha-2="CY" alpha-3="CYP" country-code="196" iso_3166-2="ISO 3166-2:CY" region="Asia" sub-region="Western Asia" intermediate-region="" region-code="142" sub-region-code="145" intermediate-region-code=""/>
            <country name="Czechia" alpha-2="CZ" alpha-3="CZE" country-code="203" iso_3166-2="ISO 3166-2:CZ" region="Europe" sub-region="Eastern Europe" intermediate-region="" region-code="150" sub-region-code="151" intermediate-region-code=""/>
            <country name="Denmark" alpha-2="DK" alpha-3="DNK" country-code="208" iso_3166-2="ISO 3166-2:DK" region="Europe" sub-region="Northern Europe" intermediate-region="" region-code="150" sub-region-code="154" intermediate-region-code=""/>
            <country name="Djibouti" alpha-2="DJ" alpha-3="DJI" country-code="262" iso_3166-2="ISO 3166-2:DJ" region="Africa" sub-region="Sub-Saharan Africa" intermediate-region="Eastern Africa" region-code="002" sub-region-code="202" intermediate-region-code="014"/>
            <country name="Dominica" alpha-2="DM" alpha-3="DMA" country-code="212" iso_3166-2="ISO 3166-2:DM" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Caribbean" region-code="019" sub-region-code="419" intermediate-region-code="029"/>
            <country name="Dominican Republic" alpha-2="DO" alpha-3="DOM" country-code="214" iso_3166-2="ISO 3166-2:DO" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Caribbean" region-code="019" sub-region-code="419" intermediate-region-code="029"/>
            <country name="Ecuador" alpha-2="EC" alpha-3="ECU" country-code="218" iso_3166-2="ISO 3166-2:EC" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="South America" region-code="019" sub-region-code="419" intermediate-region-code="005"/>
            <country name="Egypt" alpha-2="EG" alpha-3="EGY" country-code="818" iso_3166-2="ISO 3166-2:EG" region="Africa" sub-region="Northern Africa" intermediate-region="" region-code="002" sub-region-code="015" intermediate-region-code=""/>
            <country name="El Salvador" alpha-2="SV" alpha-3="SLV" country-code="222" iso_3166-2="ISO 3166-2:SV" region="Americas" sub-region="Latin America and the Caribbean" intermediate-region="Central America" region-code="019" sub-region-code="419" intermediate-region-code="013"/>
        </countries>
    </xsl:variable>

    <xsl:key name="ccode" match="country" use="@alpha-2"/>

    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- Country has the value of DK -->
    <xsl:template match="Country">
        <xsl:copy>
            <xsl:value-of select="key('ccode', ., $countries)/@alpha-3" />
        </xsl:copy>     
    </xsl:template>
</xsl:transform>

输出XML

<?xml version="1.0" encoding="utf-8"?>
<Document>
    <SalesOrder>        
        <Country>DNK</Country>       
    </SalesOrder>
</Document>

完整的国家/地区列表here