A fairly staright foward question, or so I thought...
select="../Store" returns a nodeset containing all of the nodes I need. I then need to calculate the string length of the name attribute attached to the Store node.
I would have thought it would be this:
select="string-length(../Store/@name)" but this only returns the string length of the first node.
Any ideas?
In XPath 2.0 use a single expression like this:
sum(../Store/@name/string-length(.))
This cannot be done with a single XPath 1.0 expression (a function as a location step isn't allowed), therefore some help of the hosting language is needed.
For example, if the hosting language is XSLT:
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:template match="/*">
   <xsl:apply-templates select="Store[@name][1]"/>
 </xsl:template>
 <xsl:template match="Store[@name]">
  <xsl:param name="vAccum" select="0"/>
  <xsl:value-of select="$vAccum + string-length(@name)"/>
 </xsl:template>
 <xsl:template match="Store[@name and following-sibling::Store/@name]">
  <xsl:param name="vAccum" select="0"/>
  <xsl:apply-templates select="following-sibling::Store[@name][1]">
    <xsl:with-param name="vAccum" select="$vAccum + string-length(@name)"/>
  </xsl:apply-templates>
 </xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
<root>
  <Store name="ab"/>
  <Store name="cde"/>
  <Store name="fgh"/>
  <Store name="ijklmn"/>
  <Store name="opr"/>
</root>
the wanted, correct result is produced:
17
<xsl:variable name="StoreNames">
  <xsl:for-each select="../Store">
    <xsl:value-of select="@name"/>
  </xsl:for-each>
</xsl:variable>
<xsl:value-of select="string-length($StoreNames)" />
Nice and easy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With