2017-12-21 5 views
1

このXMLからtype = cityのユニークな場所の数を取得しようとしていますが、これを行う方法がわかりません。カウントを適用しようとしていますがこれは私がXMLの例に基づいてXSLT - 複数の属性にカウントとグループを適用する

<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> 

の予想される出力aswell一意の名前と一致する必要があります、最大一致しません:

<amtCities>3</amtCities> 

答えて

2

の個別値の数を使用します

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

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

</xsl:stylesheet> 

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

0
  1. なぜ$ logパラメータを使用しているのがこのパラメータのXMLストアですか?
  2. location/@ type = "City"は4です。
  3. /root/report/location[@type='City']のみ使用できます。
+0

私は手動でのparam値を割り当てるために使用し、その細かい作業: ます。 「doc( 'File.xml')」/ ます。

+0

それは働いているが、私はすべてのユニークな都市が必要なので、アムステルダムが2回表示以上の場合、それが唯一の一つとして数え.. –

0

は、私はその細かい作業手動のparam値を割り当てるために使用します。変換とSAXON-PE-9.6.0.7のために、私は酸素を使用しています

<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> 

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> 
関連する問題