select=count(document(other.xml)/eu/other/state) : select « XSLT stylesheet « XML Tutorial






File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<eu>
 <member>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
 </member>
 <candidate>
  <state>Poland</state>
  <state>Romania</state>
  <state>Slovakia</state>
  <state>Slovenia</state>
  <state>Turkey</state>
 </candidate>
</eu>


File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />

  <xsl:template match="eu">
    <xsl:copy>
      <xsl:comment>
        <xsl:text>Member states: </xsl:text>
        <xsl:value-of select="count(member/state)" />
      </xsl:comment>
      <xsl:copy-of select="member" />
      <xsl:comment>
        <xsl:text>Candidate states: </xsl:text>
        <xsl:value-of select="count(candidate/state)" />
      </xsl:comment>
      <xsl:copy-of select="candidate" />
      <xsl:comment>
        <xsl:text>Other states: </xsl:text>
        <xsl:value-of
          select="count(document('other.xml')/eu/other/state)" />
      </xsl:comment>
      <xsl:copy-of select="document('other.xml')/eu/other" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<eu><!--Member states: 2--><member>
      <state>Austria</state>
      <state founding="yes">Belgium</state>
   </member>
   <!--Candidate states: 5--><candidate>
      <state>Poland</state>
      <state>Romania</state>
      <state>Slovakia</state>
      <state>Slovenia</state>
      <state>Turkey</state>
   </candidate>
   <!--Other states: 0--></eu>








5.66.select
5.66.1.select element by index
5.66.2.select value from attribute
5.66.3.select="@*|node()"
5.66.4.select="state[not(@founding)]"
5.66.5.select=count(document(other.xml)/eu/other/state)
5.66.6.select element by attribute value
5.66.7.Reference text after selecting tags and attributes
5.66.8.select element and attribute
5.66.9.match parent and select children
5.66.10.Match parent and select grand-children
5.66.11.Match a certain element
5.66.12.select attribute
5.66.13.Get value from an element with value-of and select
5.66.14.Select element out
5.66.15.select="operand[(. < 50) and (. > 30)]"
5.66.16.Selecting elements based on values of other ones.