Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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计算特定节点_Xml_Xslt - Fatal编程技术网

Xml 如何使用xslt计算特定节点

Xml 如何使用xslt计算特定节点,xml,xslt,Xml,Xslt,我有以下xml <color> <title>white</title> </color> <color> <title>black</title> </color> <color> <title>white</title> </color> <color> <title>black</title&g

我有以下xml

<color>
   <title>white</title>
</color>
<color>
   <title>black</title>
</color>
<color>
   <title>white</title>
</color>
<color>
   <title>black</title>
</color>
<color>
   <title>white</title>
</color>

白色
黑色
白色
黑色
白色
我需要使用
xslt
获取
color
节点的
count
,其中
title
等于“
white

ie获得结果:3


谢谢

您需要一些XSLT作为起点,并且需要有效的XML(只有一个根元素)

我需要您提供这两个函数,以便给出完整的答案,但本质上,您可以使用
count()
函数和谓词:

<xsl:value-of select="count(//color[title = 'white'])" />

更完整的示例:

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

    <xsl:template match="/">
      <n>
        <xsl:value-of select="count(//color[title = 'white'])"/>
      </n>
    </xsl:template>
</xsl:stylesheet>

解决方案:
计数(颜色[./title='white'])

请尝试以下代码

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0">
 <xsl:template match="*">
    <Value>
        <xsl:value-of select="count(color[./title='white'])"/>
    </Value>
 </xsl:template>
 </xsl:stylesheet>

输出:
3