Hi there,

I'm trying to understand how XPath works, I haven't been using it for that long :) I want to be able to select all the siblings of a specified node. For example;

<folder>
  <page id="1" />
  <page id="2" />
  <page id="3" />
  <folder id="4">
    <page id="5" />
    <page id="6" />
  </folder>
  <folder id="7">
    <page id="8" />
  </folder>
  <page id="9" />
</folder>

Example output...

[@id='5']   //gives 5,6 (all the siblings including 5)
 [@id='4']   //gives 1,2,3,4,7,9 (all the siblings including 4)
 [@id='8']   //gives just 8 (no siblings exist)

XPath query that I have so far...

/folder//child::*[@id='4']

This singles out the node I'm after, but its siblings (1,2,3...) are not also provided in the nodeset. I'm thinking that from this point I need to select the PARENT of this query, and then its CHILDREN, which will get all the sibling nodes as well. I am not sure how to put this into code, however.

Thanks for any help ;)

Alle Node testing

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output indent="yes" method="html"/>
	<xsl:template match="/">
		<html>
			<xsl:apply-templates select="folder"/>
		</html>
	</xsl:template>
	<xsl:template match="folder">
		<table>
			<xsl:for-each select="*|//@id">
				<tr>
					<td>
						<table border="solid 5px">
							<tr>
								<th>
									<xsl:value-of select="name()"/><xsl:value-of select="."/>
								</th>
							</tr>
							<tr>
								<td>child</td>
								<td>
									<xsl:value-of select="count(child::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="child::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
							<tr>
								<td>parent</td>
								<td>
									<xsl:value-of select="count(parent::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="parent::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
							<tr>
								<td>descendant</td>
								<td>
									<xsl:value-of select="count(descendant::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="descendant::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
							<tr>
								<td>following</td>
								<td>
									<xsl:value-of select="count(following::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="following::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
							<tr>
								<td>preceding</td>
								<td>
									<xsl:value-of select="count(preceding::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="preceding::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
							<tr>
								<td>following-sibling</td>
								<td>
									<xsl:value-of select="count(following-sibling::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="following-sibling::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
							<tr>
								<td>preceding-sibling</td>
								<td>
									<xsl:value-of select="count(preceding-sibling::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="preceding-sibling::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
							<tr>
								<td>descendant-or-self</td>
								<td>
									<xsl:value-of select="count(descendant-or-self::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="descendant-or-self::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
							<tr>
								<td>ancestor-or-self</td>
								<td>
									<xsl:value-of select="count(ancestor-or-self::*/@id)"/>
								</td>
								<td>
									<xsl:call-template name="print">
										<xsl:with-param name="km" select="ancestor-or-self::*/@id"/>
									</xsl:call-template>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</xsl:for-each>
		</table>
	</xsl:template>
	<xsl:template name="print">
		<xsl:param name="km"/>
		
			
		<xsl:for-each select="$km">
			<xsl:value-of select="concat(local-name(),.)"/>
			<xsl:text> </xsl:text>
		</xsl:for-each>
		
	</xsl:template>
</xsl:stylesheet>
<html>
  <table>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>page</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>8</td>
            <td>id2 id3 id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>5</td>
            <td>id2 id3 id4 id7 id9 </td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>1</td>
            <td>id1 </td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id1 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id1</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id1 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>8</td>
            <td>id2 id3 id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>8</td>
            <td>id2 id3 id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id1 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>page</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>7</td>
            <td>id3 id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>1</td>
            <td>id1 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>4</td>
            <td>id3 id4 id7 id9 </td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>1</td>
            <td>id1 </td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>1</td>
            <td>id2 </td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id2 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id2</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id2 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>7</td>
            <td>id3 id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>8</td>
            <td>id1 id3 id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id2 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>page</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>6</td>
            <td>id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>2</td>
            <td>id1 id2 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>3</td>
            <td>id4 id7 id9 </td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>2</td>
            <td>id1 id2 </td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>1</td>
            <td>id3 </td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id3 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id3</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id3 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>6</td>
            <td>id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>8</td>
            <td>id1 id2 id4 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id3 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>folder
    
    
  
            </th>
          </tr>
          <tr>
            <td>child</td>
            <td>2</td>
            <td>id5 id6 </td>
          </tr>
          <tr>
            <td>parent</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>2</td>
            <td>id5 id6 </td>
          </tr>
          <tr>
            <td>following</td>
            <td>3</td>
            <td>id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>3</td>
            <td>id1 id2 id3 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>2</td>
            <td>id7 id9 </td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>3</td>
            <td>id1 id2 id3 </td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>3</td>
            <td>id4 id5 id6 </td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id4 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id4</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id4 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>3</td>
            <td>id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>8</td>
            <td>id1 id2 id3 id5 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id4 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id5</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id5 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>4</td>
            <td>id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>7</td>
            <td>id1 id2 id3 id6 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>2</td>
            <td>id4 id5 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id6</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id6 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>3</td>
            <td>id7 id8 id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>7</td>
            <td>id1 id2 id3 id5 id7 id8 id9 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>2</td>
            <td>id4 id6 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>folder
    
  
            </th>
          </tr>
          <tr>
            <td>child</td>
            <td>1</td>
            <td>id8 </td>
          </tr>
          <tr>
            <td>parent</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>1</td>
            <td>id8 </td>
          </tr>
          <tr>
            <td>following</td>
            <td>1</td>
            <td>id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>6</td>
            <td>id1 id2 id3 id4 id5 id6 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>1</td>
            <td>id9 </td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>4</td>
            <td>id1 id2 id3 id4 </td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>2</td>
            <td>id7 id8 </td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id7 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id7</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id7 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>1</td>
            <td>id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>8</td>
            <td>id1 id2 id3 id4 id5 id6 id8 id9 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id7 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id8</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id8 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>1</td>
            <td>id9 </td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>7</td>
            <td>id1 id2 id3 id4 id5 id6 id9 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>2</td>
            <td>id7 id8 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>page</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>8</td>
            <td>id1 id2 id3 id4 id5 id6 id7 id8 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>5</td>
            <td>id1 id2 id3 id4 id7 </td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>1</td>
            <td>id9 </td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id9 </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table border="solid 5px">
          <tr>
            <th>id9</th>
          </tr>
          <tr>
            <td>child</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>parent</td>
            <td>1</td>
            <td>id9 </td>
          </tr>
          <tr>
            <td>descendant</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>following</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding</td>
            <td>8</td>
            <td>id1 id2 id3 id4 id5 id6 id7 id8 </td>
          </tr>
          <tr>
            <td>following-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>preceding-sibling</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>descendant-or-self</td>
            <td>0</td>
            <td></td>
          </tr>
          <tr>
            <td>ancestor-or-self</td>
            <td>1</td>
            <td>id9 </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</html>

Helmut Hagemann

This article has been dead for over six months. Start a new discussion instead.