Re: PHP4, domxml and xpath with Namespaces

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Markus Fischer wrote:
Hi,

I haven't yet worked with namespaces in XML before when it comes to DOM
related methods. My source is an Office 2003 Excel XML and it contains
XML like:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40";>
 <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"/>
 <Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="256" ss:ExpandedRowCount="670"
x:FullColumns="1"
   x:FullRows="1" ss:DefaultColumnWidth="60">
   <Column ss:StyleID="s22" ss:AutoFitWidth="0" ss:Width="284.25"/>
   <Row>

How can I use an xpath expression to find e.g. all rows in Worksheet
"Sheet1" ?

My problem here is that the name of the Worksheet is in a namespace
attribute and I don't know how to express this as an xpath expression.

The following doesn't work, always returns an empty nodeset to me:
Worksheet[@ss:Name='Sheet1']

Can someone give me a push into the right direction?

You need to register the namespace and a prefix (prefix can be anything you want to use) with xpath and don't forget what the elements are within a default namespace coming from the xmlns="urn:schemas-microsoft-com:office:spreadsheet" namespace declaration on the Workbook element.

xpath_register_ns($xpath,"ss", "urn:schemas-microsoft-com:office:spreadsheet");

$nodeset = xpath_eval($xpath,"ss:Worksheet[@ss:Name='Sheet1']");

both the element and attribute are within the same namespace so the namespace and prefix are registered once and the prefix is used to identify both in the xpath expression.

Rob

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux