Using Wildcards in XPath Expressions for Flexible Node Selection in XML

Learn how to utilize wildcards in XPath expressions to select nodes in XML documents without specifying exact node names. This tutorial explains different wildcard characters and provides practical examples demonstrating how to create more robust and flexible XPath queries for data extraction and manipulation.



Using Wildcards in XPath Expressions

XPath wildcards provide a flexible way to select nodes in an XML document without needing to specify the exact node names. This is very useful when you don't know the precise names of the nodes or when you want to select a group of nodes based on a pattern. Wildcards make your XPath expressions more concise and robust.

XPath Wildcard Characters

XPath uses several wildcard characters:

Wildcard Description
* Matches any element node.
. Matches the current node.
@* Matches any attribute.
node() Matches any node of any type.

Example: Selecting Employee Details Using Wildcards

(Note: This example requires an XML file named `employee.xml` and an XSLT stylesheet named `employee.xsl`. Screenshots from the original text are not included here. Please refer to the original document for visual verification of the example and XSLT output. The descriptions below aim to convey the information present in those screenshots.)

This example shows how to use wildcards in an XSLT stylesheet to select and display employee data from an XML file.

`employee.xml` (Example XML Data)


<employees>
  <employee>
    <firstName>Abhiram</firstName>
    <lastName>Kushwaha</lastName>
    <nickName>Manoj</nickName>
    <salary>15000</salary>
  </employee>
  <!-- ... more employee elements ... -->
</employees>

`employee.xsl` (XSLT Stylesheet)

(The original content includes the content of the `employee.xsl` file. Since we cannot display the file here, please refer to the original document for the file content.)

Test it Now

(The original content includes a screenshot showing the output. Since we cannot display images, please refer to the original document for the visual representation of the output. The description below aims to convey the information in the screenshot.)

The output shows an HTML table. The XSLT uses wildcards in its XPath expressions to select all the employee details. The use of wildcards makes the XPath expression more concise and flexible.