Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update xml file with php Xpath

Tags:

php

xml

xslt

xpath

How to use use Xpath (php) update file? My file structure:

<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonList>
  <Person>
    <Name>Sonu Kapoor</Name>
    <Age>24</Age>
    <Gender>M</Gender>
    <PostalCode>54879</PostalCode>
  </Person>
  <Person>
    <Name>Jasmin</Name>
    <Age>28</Age>
    <Gender>F</Gender>
    <PostalCode>78745</PostalCode>
  </Person>
   <Person>
    <Name>Josef</Name>
    <Age>232</Age>
    <Gender>F</Gender>
    <PostalCode>53454</PostalCode>
  </Person>
</PersonList>

And i need change values Age and Gender where name is "Jasmin". I try use google, but nothing good no found :(

like image 645
lolalola Avatar asked Dec 22 '10 05:12

lolalola


2 Answers

You can try simplexml

$xml = simplexml_load_string($str);
$obj = $xml->xpath('//Person[Name="Jasmin"]');
$obj[0]->Age = 30;
$obj[0]->Gender = 'M';
echo $xml->asXml();

/* or */ 
$xml->asXml($filename); <-- save xml into file
like image 197
ajreal Avatar answered Oct 22 '22 12:10

ajreal


How to use use Xpath (php) update file?

XPath is a query language for XML documents. As such, an XPath expression cannot modify an XML document -- it can only select nodes or other data from it.

A modified XML document can be produced with the help of the programming language that is hosting the XPath engine -- this may be XSLT, C#, Java, PHP, ...

And i need change values Age and Gender where name is "Jasmin".

Here is a simple XSLT transformation that produces a new XML document according to these requirements:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:params>
  <name>Jasmin</name>
  <age>31</age>
  <gender>X</gender>
 </my:params>

 <xsl:variable name="vParams" select=
 "document('')/*/my:params"/>

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match=
  "Person[Name=document('')/*/my:params/name]/Age">
  <Age><xsl:value-of select="$vParams/age"/></Age>
 </xsl:template>

 <xsl:template match=
  "Person[Name=document('')/*/my:params/name]/Gender">
  <Gender><xsl:value-of select="$vParams/gender"/></Gender>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<PersonList>
    <Person>
        <Name>Sonu Kapoor</Name>
        <Age>24</Age>
        <Gender>M</Gender>
        <PostalCode>54879</PostalCode>
    </Person>
    <Person>
        <Name>Jasmin</Name>
        <Age>28</Age>
        <Gender>F</Gender>
        <PostalCode>78745</PostalCode>
    </Person>
    <Person>
        <Name>Josef</Name>
        <Age>232</Age>
        <Gender>F</Gender>
        <PostalCode>53454</PostalCode>
    </Person>
</PersonList>

the wanted, correct result is produced:

<PersonList>
   <Person>
      <Name>Sonu Kapoor</Name>
      <Age>24</Age>
      <Gender>M</Gender>
      <PostalCode>54879</PostalCode>
   </Person>
   <Person>
      <Name>Jasmin</Name>
      <Age>31</Age>
      <Gender>X</Gender>
      <PostalCode>78745</PostalCode>
   </Person>
   <Person>
      <Name>Josef</Name>
      <Age>232</Age>
      <Gender>F</Gender>
      <PostalCode>53454</PostalCode>
   </Person>
</PersonList>
like image 4
Dimitre Novatchev Avatar answered Oct 22 '22 13:10

Dimitre Novatchev