Using
//node() | //@* | //namespace::*
this selects any node (document type node / , element node, node text, node processing instruction and comment node) and any node attribute and any node namespace are all nodes because there are no other types of nodes.
How you access the resulting XmlNodeList containing the selected nodes depends on the API of the particular XPath engine that you use — read and use your documentation.
XSLT Example :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <xsl:for-each select= "//node() | //@* | //namespace::*"> Type: <xsl:text/> <xsl:choose> <xsl:when test="not(..)"> <xsl:text>document node </xsl:text> </xsl:when> <xsl:when test="self::*"> <xsl:text>element </xsl:text> </xsl:when> <xsl:when test="self::text()"> <xsl:text>text-node </xsl:text> </xsl:when> <xsl:when test="self::comment()"> <xsl:text>comment-node </xsl:text> </xsl:when> <xsl:when test="self::processing-instruction()"> <xsl:text>PI-node </xsl:text> </xsl:when> <xsl:when test="count(.|../@*) = count(../@*)"> <xsl:text>attribute-node </xsl:text> </xsl:when> <xsl:when test= "count(.|../namespace::*) = count(../namespace::*)"> <xsl:text>namespace-node </xsl:text> </xsl:when> </xsl:choose> <xsl:text>Name: "</xsl:text> <xsl:value-of select="name()"/>" <xsl:text/> <xsl:text>Value: </xsl:text> <xsl:value-of select="."/> </xsl:for-each> </xsl:template> </xsl:stylesheet>
when this XSLT transformation is applied to any XML document, it selects all nodes using the above XPath expression (the conversion intentionally excludes any text nodes for space only) and displays (in document order) the type, name and string value of the selected nodes .
For example, when applying this XML document :
<networkOfBridges xmlns:x="x"> <bridge id="1" otherside="A" /> <bridge id="2" oneside="A"/> <?PI Processing Instruction ?> <bridge id="3" oneside="A" otherside="A" /> </networkOfBridges>
result :
Type: element Name: "networkOfBridges" Value: Type: namespace-node Name: "xml" Value: http://www.w3.org/XML/1998/namespace Type: namespace-node Name: "x" Value: x Type: element Name: "bridge" Value: Type: namespace-node Name: "xml" Value: http://www.w3.org/XML/1998/namespace Type: namespace-node Name: "x" Value: x Type: attribute-node Name: "id" Value: 1 Type: attribute-node Name: "otherside" Value: A Type: comment-node Name: "" Value: A Comment Type: element Name: "bridge" Value: Type: namespace-node Name: "xml" Value: http://www.w3.org/XML/1998/namespace Type: namespace-node Name: "x" Value: x Type: attribute-node Name: "id" Value: 2 Type: attribute-node Name: "oneside" Value: A Type: PI-node Name: "PI" Value: Processing Instruction Type: element Name: "bridge" Value: Type: namespace-node Name: "xml" Value: http://www.w3.org/XML/1998/namespace Type: namespace-node Name: "x" Value: x Type: attribute-node Name: "id" Value: 3 Type: attribute-node Name: "oneside" Value: A Type: attribute-node Name: "otherside" Value: A
source share