使用XSLT进行地图迭代

时间:2019-03-29 01:29:03

标签: xml dictionary xslt xpath iteration

我正在尝试使用XSLT将值作为“ xpath”的Map(键,值对)进行迭代。但是我无法使用xslt 2.0迭代地图条目。

地图:

<xsl:variable name="map">
 <entry key="Access">//DataService [@type ='PR_ACC']/ID</entry>
 <entry key="Transport">//DataService [@type ='PR_IB']/ID</entry>
 <entry key="CHE">//EquipmentService [@type ='PR_CPE']/ID</entry>
 </xsl:variable>

循环:

<Id><xsl:value-of select="$map/entry[@key=ID/@schemeID]"/></Id>

xpath:

ID/@schemeID will return any one of (Access, Transport, CPE)

样本输入XML:

<ID schemeID="CHE"></ID>
<Services>
       <DataService type ='PR_ACC'>
             <ID>12345<ID>
       </DataService>
       <DataService type ='PR_BCC'>
               <ID>12345<ID>  
      </DataService>
       <EquipmentService type =' PR_CPE  '>
                <ID>98765<ID>      
     </EquipmentService>
</Services>

预期输出:

<Id> 98765 </Id>

有人告诉我我想念什么吗?

1 个答案:

答案 0 :(得分:0)

假设XSLT 3具有XPath 3.1支持并支持xsl:evaluate(例如,自9.8及更高版本开始在Saxon PE和EE中提供),您可以编写类似

的代码
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:variable name="map" as="map(xs:string, xs:string)"
        select="map {
        'Access' : &quot;//DataService [@type ='PR_ACC']/ID&quot;,
        'Transport' : &quot;//DataService [@type ='PR_IB']/ID&quot;,
        'CHE' : &quot;//EquipmentService [@type ='PR_CPE']/ID&quot;
        }"/>

    <xsl:template match="root">
        <xsl:copy>
            <ID>
                <xsl:evaluate xpath="$map(ID/@schemeID)" context-item="/"/>
            </ID>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

在针对更正的输入运行时

<root>
    <ID schemeID="CHE"></ID>
    <Services>
        <DataService type='PR_ACC'>
            <ID>12345</ID>
        </DataService>
        <DataService type='PR_BCC'>
            <ID>12345</ID>  
        </DataService>
        <EquipmentService type='PR_CPE'>
            <ID>98765</ID>      
        </EquipmentService>
    </Services>
</root>

输出<root><ID><ID>98765</ID></ID></root>