tags:

views:

81

answers:

1

hi, how can i add namespace name before a attribute of newly created tag,like

<Data ss:Type="String">this the value of data</Data>

but can create only

<Data Type="String">this the value of data</Data>

so i can not add "ss:" before atttbute.

Thanks in Advance

Have Dream Day

A: 

In the SimpleXml PHP manual someone gave a good example in the comments section.

Here is how I got it to work.

Say you have the following XML in a file xml.php:

<?php
$string = <<<XML
<Row>
  <Cell>
    <Data>Dolly Parton</Data>
  </Cell>
  <Cell>
    <Data>Islands in the Stream</Data>
  </Cell>
  <Cell>
    <Data>what</Data>
  </Cell>
  <Cell>
    <Data>1-29-2010</Data>
  </Cell>
</Row>
XML;

You can read in this file and add the attributes you want by using XPath.

 <?php 
        include("xml.php"); 

        $sxe = new SimpleXMLElement($string);

        $data = $sxe->xpath('//Data');

    foreach ($data as $value)
    {
        $value->addAttribute('ss:Type', 'String', 'http://www.w3.org/2001/XMLSchema-instance'); 
        }

        echo $sxe->asXML();

    ?>

This is the output I get:

<Row>
  <Cell>
    <Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">Dolly Parton</Data>
  </Cell>
  <Cell>
    <Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">Islands in the Stream</Data>
  </Cell>

  <Cell>
    <Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">what</Data>
  </Cell>
  <Cell>
    <Data xmlns:ss="http://www.w3.org/2001/XMLSchema-instance" ss:Type="String">1-29-2010</Data>
  </Cell>
</Row>
Jason Rowe
hi Thanks you very much but still it is not working ,i can give code i want somthing like this<Row> <Cell><Data ss:Type="String">Dolly Parton</Data></Cell> <Cell><Data ss:Type="String">Islands in the Stream</Data></Cell> <Cell><Data ss:Type="String">254.233.11.33</Data></Cell> <Cell><Data ss:Type="String">1-29-2010</Data></Cell> </Row>and i try with$data->addAttribute("ss:Type","String",'urn:schemas-microsoft-com:office:spreadsheet');but still not working plz help outhave dream day
rubyid10
Ok, I added more code to my answer. Let me know if I am on the right path. Are you working in PHP or not?
Jason Rowe
yes i am working in php,thanks for help but i get by simpleDOM class in google code,then i copy the one element attribute to another,now i have amother query how to edit attributes values that have the namesapce,when i try to edit it create the new attribute without the namespace$table[0]->setAttributes(array('ExpandedRowCount'=>5))that output somthing like<Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="4" x:FullColumns="1" x:FullRows="1" ss:DefaultColumnWidth="54" ExpandedRowCount="5">Thanks for everthinghave dream day
rubyid10
I think you are going to need to update the original question with more code. I couldn't find any documentation on setAttributes() to determine how to add in the namespace. Post more code in the original questions and I'll try to take a look.
Jason Rowe