如何从XSLT2.0中的国家名称获取ISO国家代码
在XSLT2.0中是否有任何内置函数或方法来获取ISO国家代码?。我在请求中得到一个国家名称,我必须在发出的请求中传递国家代码。一种方法是将每个国家的名称映射到其各自的国家代码,但是这需要很多时间,所以有没有捷径 是否有任何内置函数或方法可以在XSLT中获取ISO国家代码 2.0 不,当然不是。使用查找表 一种方法是将每个国家的名称映射到各自的国家代码, 然而,这将需要很多时间,所以有没有捷径呢 那个 已经完成了,例如:如何从XSLT2.0中的国家名称获取ISO国家代码,xslt,xslt-1.0,xslt-2.0,Xslt,Xslt 1.0,Xslt 2.0,在XSLT2.0中是否有任何内置函数或方法来获取ISO国家代码?。我在请求中得到一个国家名称,我必须在发出的请求中传递国家代码。一种方法是将每个国家的名称映射到其各自的国家代码,但是这需要很多时间,所以有没有捷径 是否有任何内置函数或方法可以在XSLT中获取ISO国家代码 2.0 不,当然不是。使用查找表 一种方法是将每个国家的名称映射到各自的国家代码, 然而,这将需要很多时间,所以有没有捷径呢 那个 已经完成了,例如: 如果您有以下数据 <codes> <country
如果您有以下数据
<codes>
<country code="gb" name="United Kingdom"/>
<country code="de" name="Germany"/>
...
</codes>
哪个返回“比利时”
其中,$country code是包含此数据的文档。以防其他人在试图解决与原始海报相同的问题时发现此问题。我能够利用@Michael Kay提供的所有细节来解决这个问题 源XML
<?xml version="1.0" encoding="utf-8"?>
<Document>
<SalesOrder>
<Country>DK</Country>
</SalesOrder>
</Document>
<?xml version="1.0" encoding="utf-8"?>
<Document>
<SalesOrder>
<Country>DNK</Country>
</SalesOrder>
</Document>
DK
XSLT/Stylesheet
<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>DK</Country>
</SalesOrder>
</Document>
<?xml version="1.0" encoding="utf-8"?>
<Document>
<SalesOrder>
<Country>DNK</Country>
</SalesOrder>
</Document>
DNK
当你说“很多时间”时,你指的是你的时间还是电脑时间?它不应该占用您太多的时间:只需下载,也不应该占用太多的机器时间:只需定义一个键。@omerkhalid我为这一困惑道歉,我的意思是,在XSLT中,我是否必须应用这么多匹配的If或Switch条件,或者是否有任何快捷方式可以做到这一点?在XSLT中,您可以使用从表(可以在外部XML文档中)中查找匹配的国家/地区。