Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用xslt将countrycode转换为countryname_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

Xml 使用xslt将countrycode转换为countryname

Xml 使用xslt将countrycode转换为countryname,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我试图从CountryCode的位置获取CountryName的值。例如,在XML文件中 <CountryCode>USA</CountryCode> 美国 XSLT代码: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes

我试图从CountryCode的位置获取CountryName的值。例如,在XML文件中

<CountryCode>USA</CountryCode>
美国
XSLT代码:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="Party">
    <xsl:copy-of select="node()|@*"/>
</xsl:template>
<xsl:template match="CountryCode">
    <xsl:copy>
        <xsl:variable name="CountryCode" select="upper-case(.)=('HK','USA','SG')"/> // list of country codes
        <xsl:variable name="CountryName" as="element()*"> // list of countryname
            <Name>HongKong</Name>
            <Name>United States of America</Name>
            <Name>Singapore</Name>
        </xsl:variable>
        <xsl:value-of select="$CountryName[($CountryCode)]"/> // get the position or index of the variable countryname to variable countrycode
    </xsl:copy>
</xsl:template>

//国家代码清单
//国名表
香港
美利坚合众国
新加坡
//获取变量countryname到变量countrycode的位置或索引

但是,我得到了CountryName的所有价值。像这样,

<CountryCode>Philippines United States of America Singapore</CountryCode>
菲律宾美利坚合众国新加坡
而不是美国

我的代码中缺少什么吗?还是我做得不对

提前谢谢


XSLT2.0

由于您使用的是XSLT2.0,因此可以使用
index of
函数从
$CountryCode
变量中获取
CountryCode
的索引,然后使用索引值从
$CountryName
中获取
CountryName

请尝试修改模板匹配
,如下所示

<xsl:template match="CountryCode">
    <!-- Store input in a variable -->
    <xsl:variable name="InputCode" select="." />
    <xsl:copy>
        <!-- Removed the upper-case function and retained the sequence -->
        <xsl:variable name="CountryCode" select="('HK','USA','SG')"/>

        <xsl:variable name="CountryName" as="element()*">
            <Name>HongKong</Name>
            <Name>United States of America</Name>
            <Name>Singapore</Name>
        </xsl:variable>

        <!-- Added the index-of function to get the index of the input country
             code from the sequence and then extract the country name from the
             other sequence -->
        <xsl:value-of select="$CountryName[index-of($CountryCode, upper-case($InputCode))]"/>
    </xsl:copy>
</xsl:template>

香港
美利坚合众国
新加坡
输入

<CountryCode>USA</CountryCode>
美国
上面的模板将给出如下输出:

<CountryCode>United States of America</CountryCode>
美利坚合众国

由于您使用的是XSLT 2.0,因此可以使用
index of
函数从
$CountryCode
变量中获取
CountryCode
的索引,然后使用索引值从
$CountryName
中获取
CountryName

请尝试修改模板匹配
,如下所示

<xsl:template match="CountryCode">
    <!-- Store input in a variable -->
    <xsl:variable name="InputCode" select="." />
    <xsl:copy>
        <!-- Removed the upper-case function and retained the sequence -->
        <xsl:variable name="CountryCode" select="('HK','USA','SG')"/>

        <xsl:variable name="CountryName" as="element()*">
            <Name>HongKong</Name>
            <Name>United States of America</Name>
            <Name>Singapore</Name>
        </xsl:variable>

        <!-- Added the index-of function to get the index of the input country
             code from the sequence and then extract the country name from the
             other sequence -->
        <xsl:value-of select="$CountryName[index-of($CountryCode, upper-case($InputCode))]"/>
    </xsl:copy>
</xsl:template>

香港
美利坚合众国
新加坡
输入

<CountryCode>USA</CountryCode>
美国
上面的模板将给出如下输出:

<CountryCode>United States of America</CountryCode>
美利坚合众国

如果您的国家代码是ISO 3166代码,并且您可以使用XSLT 3.0处理器,那么您只需执行以下操作即可

<xsl:template match="CountryCode">
  <xsl:copy>{json-doc('http://country.io/names.json')(.)}</xsl:copy>
</xsl:template>

{json doc('http://country.io/names.json')(.)}

如果您没有使用ISO 3166代码,或者您没有使用XSLT 3.0,那么您可能会被说服更改…

如果您的国家代码是ISO 3166代码,并且如果您可以使用XSLT 3.0处理器,那么您可以简单地进行更改

<xsl:template match="CountryCode">
  <xsl:copy>{json-doc('http://country.io/names.json')(.)}</xsl:copy>
</xsl:template>

{json doc('http://country.io/names.json')(.)}
如果您没有使用ISO 3166代码,或者您没有使用XSLT 3.0,那么也许可以说服您更改