3

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>
2
  • Could it be issue related to namespace? The PeopleXml.xml does not have any namespace but the xslt has 'xmlns="MyNamespace"'. Commented Dec 12, 2012 at 9:26
  • For the first xapth issue (where I was trying to compare Ids), the solution provided by Daniel is working. Now, I just want to write xpath which will Check if we have atleast one developer". The xapth I tried is $peopleList/Person/Profession/Role=$roleDev Commented Dec 12, 2012 at 9:29

2 Answers 2

2

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"

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

1 Comment

Thanks Daniel. It worked! However, if i have to access any element inside person (e.g. Role), then its giving problem. I have updated the xml and xpath in question. Any suggestion?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.