Open In App

XPath Wildcard

Last Updated : 17 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

XPath expressions consist of wildcards which constitute an important part in matching part of expressions.

Below are the different symbols used as wildcard expressions.

1. Symbol:  * :- It is used for matching any given node.
2. Symbol: . :- Used for matching the current node which is taken into consideration context.
3. Symbol: @*:- It is used for matching any attribute.
4. Symbol: node(): - It is used for matching the node of any given type.

The following example depicts the use of wildcards and the different symbols that constitute the wildcards that were used on nodes within the XPath expression:

Example 1: Class with three students and their unique descriptions. Each student is described with three unique characteristics name, age, and birth year.

XML




//classgfg.xml
  
<?xml version="1.0"  encoding="UTF-8"?> 
<?xml-stylesheet type = "text/xsl" href = "classgfg.xsl"?> 
<class>  
   <student id = "01">  
      <name>Amrit</name>  
      <age>8</age>  
      <birthyear>2015</birthyear>   
   </student>  
   <student id = "02">  
      <name>Vaibhav</name>  
      <age>8</age>  
      <birthyear>2015</birthyear>   
   </student>  
   <student id = "03">  
      <name>Shashi</name>  
      <age>9</age>  
      <birthyear>2014</birthyear>   
   </student>   
</class>


XML




//student.xsl
  
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" 
                xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">
   <xsl:template match = "/">
      <html>
         <body>
            <xsl:apply-templates select = "class/*" />
         </body>
      </html>
   </xsl:template>
  
   <xsl:template match = "class/*">
      <xsl:apply-templates select = "@id" />
      <xsl:apply-templates select = "name" />
      <xsl:apply-templates select = "age" />
      <xsl:apply-templates select = "birthyear" />
      <br />
   </xsl:template>
  
<xsl:template match = "@id">
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
   <xsl:template match = "name">
      name:
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
   <xsl:template match = "age">
      age:
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
   <xsl:template match = "birthyear">
      birthyear:
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
</xsl:stylesheet>


Output:

Screenshot-2023-09-17-at-92537-PM

figure 1

Example 2: Student are given choice to opt for 4 different subjects. Each student is then tagged with 4 different subjects of their choice.

XML




//subject.xml
  
<?xml version="1.0"  encoding="UTF-8"?>
<?xml-stylesheet type = "text/xsl" href = "subject.xsl"?> 
<class>  
   <student id = "01">  
      <subject1> Maths </subject1>  
      <subject2> English </subject2>  
      <subject3> French </subject3
      <subject4> Physics </subject4>  
   </student>  
   <student id = "02">  
      <subject1> Chemistry </subject1>  
      <subject2> Maths </subject2>  
      <subject3> Physics </subject3>
      <subject4> English </subject4>   
   </student>
   <student id = "03">  
      <subject1> English </subject1>  
      <subject2> Chemistry </subject2>  
      <subject3>Maths </subject3>
      <subject4>Physics </subject4>   
   </student>
   <student id = "04">  
      <subject1> Biology </subject1>  
      <subject2> English </subject2>  
      <subject3> Physics </subject3>
      <subject4> Chemistry </subject4>   
   </student>      
   <student id = "05">  
      <subject1> Hindi </subject1>  
      <subject2> English </subject2>  
      <subject3> Physics </subject3>
      <subject4> Biology </subject4>   
   </student>   
</class>


XML




//subject.xsl
  
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   <xsl:template match = "/">
      <html>
         <body>
            <xsl:apply-templates select = "class/*" />
         </body>
      </html>
   </xsl:template>
  
   <xsl:template match = "class/*">
      <xsl:apply-templates select = "@id" />
      <xsl:apply-templates select = "subject1" />
      <xsl:apply-templates select = "subject2" />
      <xsl:apply-templates select = "subject3" />
      <xsl:apply-templates select = "subject4" />
      <br />
   </xsl:template>
  
<xsl:template match = "@id">
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
   <xsl:template match = "subject1">
      subject1:
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
   <xsl:template match = "subject2">
      subject2:
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
  <xsl:template match = "subject3">
      subject3:
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
  <xsl:template match = "subject4">
      subject4:
         <xsl:value-of select = "." />
      <br />
   </xsl:template>
  
</xsl:stylesheet>


Output:

Screenshot-2023-09-17-at-110302-PM

figure 2



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads