XSLT - 对多个属性应用计数和分组

时间:2017-12-21 08:50:57

标签: xml xslt

我正在尝试从这个XML中获取type = city的唯一位置数量,但不知道如何执行此操作..我一直在尝试应用计数,但这不匹配,我会需要匹配一个唯一的名称以及

<root>
    <report>
        <location name="Amsterdam" type="City">
            <amtPeople>1 Million+</amtPeople>
            <date>21-12-2017</date>
        </location>
        <location name="London" type="City">
            <amtPeople>1 Million+</amtPeople>
            <date>21-12-2017</date>
        </location>
        <location name="Boekelo" type="Village">
            <amtPeople>1 Million+</amtPeople>
            <date>21-12-2017</date>
        </location>
    </report>
    <report>
        <location name="Amsterdam" type="City">
            <amtPeople>1 Million+</amtPeople>
            <date>14-12-2017</date>
        </location>
        <location name="New York" type="City">
            <amtPeople>1 Million+</amtPeople>
            <date>14-12-2017</date>
        </location>
        <location name="Capelle" type="Village">
            <amtPeople>1 Million+</amtPeople>
            <date>14-12-2017</date>
        </location>
    </report>
</root>

XSLT

<xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:param name="Log"/>
<xsl:variable name="AllCities" select="count($Log/root/report/location[@type='City'])"/>

<xsl:template match="/">
    <amtCities><xsl:value-of select="$AllCities"/></amtCities>
</xsl:template>

</xsl:stylesheet>   

基于XML示例的预期输出:

<amtCities>3</amtCities>

4 个答案:

答案 0 :(得分:2)

使用不同值的计数:

import { CoursesComponent } from './Courses.Component';

http://xsltfiddle.liberty-development.net/948Fn57

答案 1 :(得分:0)

  1. 为什么使用$ log参数是这个参数中的XML存储?
  2. 我注意到位置/ @ type =&#34; City&#34;是4.
  3. 您只能使用/root/report/location[@type='City']

答案 2 :(得分:0)

我用手动分配参数值,其工作正常:

<xsl:param name="Log" select="doc('File.xml')"/>

<xsl:variable name="AllCities" select="count($Log/root/report/location[@type='City'])"/>

<xsl:template match="/">
<amtCities><xsl:value-of select="$AllCities"/></amtCities>
</xsl:template>

我正在使用氧气进行转换和SAXON-PE-9.6.0.7。

答案 3 :(得分:0)

<xsl:param name="Log" select="doc('File.xml')"/>

    <xsl:template match="/">
        <amtcities>
            <xsl:value-of select="count(distinct-values($Log/root/report/location[@type='City']/@name))"/>
        </amtcities>
    </xsl:template>
相关问题