Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath is not working on variable having xml string

I am facing issue in writing xapth. Let me explain the problem.

I am writing xslt to transform some xml. The xslt also loads one xml file from disk into xslt variable.

PeopleXml.xml:

   <TestXml>     
     <People>  
       <Person id="MSA1" name="Sachin">
         <Profession>  
           <Role>Developer</Role>
         </Profession>  
       </Person>
       <Person id="ZAG4" name="Rahul">              
         <Profession>  
           <Role>Tester</Role>
          </Profession> 
       </Person>
     </People>  
   </TestXml>  

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xmlns="http://MyNamespace"  
version="2.0"> 

<xsl:variable name="PeopleXml" select ="document('PeopleXml.xml')"/>

<xsl:variable name="peopleList" select="$PeopleXml/TestXml/People/Person"/>  
<xsl:variable name="person1" select="MSA1"/>  

<xsl:variable name="person" select="$peopleList/Person[@id=$person1]/@name"/>  

<xsl:template match="/">
   <xsl:value-of select="$person"/>
</xsl:template>

</xsl:stylesheet>

Issue: The xpath "$peopleList/Person[@id=$person1]/@name" is not returning anything. Infact, $peopleList/Person also does not work. However, I can see two person nodes in $peopleList variable when I debugged the code.

Could anyone help me, what I am doing wrong in xpath?

EDIT Above xapth issue has been resolved after applying Daniel's solution. Now, only issue remained is with accessing child nodes of person based on some condition.

Following test does not work.

<xsl:variable name="roleDev" select="'Developer'"/>
<xsl:when test="$peopleList/Profession/Role=$roleDev">
   <xsl:value-of select="We have atleast one Developer"/>
</xsl:when>
like image 261
Sambhaji Avatar asked Jul 03 '26 11:07

Sambhaji


2 Answers

Your problem is here:

<xsl:variable name="person1" select="MSA1"/>

This results in having the $person1 variable empty. Why?

Because the expression MSA1 is evaluated -- the current node doesn't have any children named "MSA1" and nothing is selected.

Solution:

Specify the wanted string as string literal:

<xsl:variable name="person1" select="'MSA1'"/>

Your Second Question:

Now, only issue remained is with accessing child nodes of person based on some condition.

Use:

boolean($peopleList[Profession/Role = 'Developer'])

This produces true() exactly when there is a node in $peopleList such that it has at least one Profession/Role crand-child whose string value is the string "Developer"

like image 157
Dimitre Novatchev Avatar answered Jul 05 '26 15:07

Dimitre Novatchev


Since the variable peopleList is already Person nodes, you should access them like this:

<xsl:variable name="person" select="$peopleList[@id=$person1]/@name"/>
like image 45
Daniel Haley Avatar answered Jul 05 '26 15:07

Daniel Haley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!