943,790 Members | Top Members by Rank

Ad:
Aug 27th, 2009
-1

[PHP] How to change a value with XPath

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tryhere is offline Offline
1 posts
since Aug 2009
Sep 9th, 2009
0

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

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.
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: XPath expression for <td> tag having "Type" as textContent in any of its childNodes
Next Thread in XML, XSLT and XPATH Forum Timeline: my swf not loading xml at server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC