Reputation: 103
I have a xml file and corresponding xsl file. I have a following code line which is repeated lot of times in a code. I need to find how many times the value for status is 0 ? How can i do this ?
Thanks in advance
Upvotes: 0
Views: 158
Reputation: 243599
The XML document is in a default namespace.
So, the solution must take this into account:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.plmxml.org/Schemas/PLMXMLSchema">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="count(//x:UserValue[@title = 'status' and @value= '0'])"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
the XPath expression whic returns the count of the specified nodes is evaluated and this count is output:
2
Do note: XPath expressions that use the //
pseudo-operator can be very slow, therefore if the structure of the XML document is well-known one should use an equivalent XPath expression that doesn't contain //
.
For example, if I have correctly understood the structure of the provided document, this is such a better XPath expression:
count(/*/*/x:UserData/x:UserValue[@title = 'status' and @value= '0'])
Upvotes: 1
Reputation: 12154
Input XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<child1>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="statusss"></UserValue>
</child1>
<child2>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
<UserValue type="int" value="0" title="status"></UserValue>
</child2>
</root>
This will count the number of UserValue
nodes in whole file which are having attribute Value='0'
and attribute title='status'
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://www.plmxml.org/Schemas/PLMXMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="count_nodes" select="count(//ns:UserValue[@value='0' and @title='status'])"/>
<xsl:template match="/">
<xsl:element name="count">
<xsl:value-of select="$count_nodes"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="utf-8"?>
<Count>2</Count>
EDITED as per Dimitre's comment on performance difference:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="http://www.plmxml.org/Schemas/PLMXMLSchema">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="count_nodes" select="count(/*/*/ns:UserData/ns:UserValue[@title = 'status' and @value= '0'])"/>
<xsl:template match="/">
<xsl:element name="count">
<xsl:value-of select="$count_nodes"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2
Reputation: 167716
Use the xpath count(//UserValue[@title = 'status' and @value = 0])
.
Upvotes: 2