[PHP] How to change a value with XPath

Please support our XML, XSLT and XPATH advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2009
Posts: 1
Reputation: tryhere is an unknown quantity at this point 
Solved Threads: 0
tryhere tryhere is offline Offline
Newbie Poster

[PHP] How to change a value with XPath

 
-1
  #1
Aug 27th, 2009
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0"?>
  2. <staff>
  3. <user>
  4. <id>A</id>
  5. <rank>User</rank>
  6. </user>
  7. <user>
  8. <id>B</id>
  9. <rank>User</rank>
  10. </user>
  11. <user>
  12. <id>C</id>
  13. <rank>Administrator</rank>
  14. </user>
  15. </staff>
I have to promote to "Administrator" a random user using XPath.
How can I do it?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 95
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 5
fpmurphy fpmurphy is offline Offline
Junior Poster in Training

Re: [PHP] How to change a value with XPath

 
0
  #2
Sep 9th, 2009
Here is a stylesheet which will do what you want
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  3.  
  4. <!-- pass in as -param uid "'value'" -->
  5. <xsl:param name="uid"/>
  6.  
  7. <xsl:output method="xml" indent="yes"/>
  8.  
  9. <xsl:template match="/">
  10. <xsl:element name="staff">
  11. <xsl:apply-templates select="//user"/>
  12. </xsl:element>
  13. </xsl:template>
  14.  
  15. <!-- output nodes that match -->
  16. <xsl:template match="user">
  17. <xsl:if test="id=$uid">
  18. <xsl:element name="user">
  19. <xsl:element name="id"><xsl:value-of select="$uid"/></xsl:element>
  20. <xsl:element name="rank">Administrator</xsl:element>
  21. </xsl:element>
  22. </xsl:if>
  23. <xsl:if test="id!=$uid">
  24. <xsl:copy-of select="."/>
  25. </xsl:if>
  26. </xsl:template>
  27.  
  28. </xsl:stylesheet>
Last edited by fpmurphy; Sep 9th, 2009 at 12:01 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC