API响应中有一些烦人的XML,如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><report><QueryResult>
<ResumptionToken>123456</ResumptionToken>
<IsFinished>true</IsFinished>
<ResultXml>
<rowset xmlns="urn:schemas-microsoft-com:xml-analysis:rowset">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:saw-sql="urn:saw-sql" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset">
<xsd:complexType name="Row">
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="Column0" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:columnHeading="0" saw-sql:displayFormula="0" saw-sql:length="4" saw-sql:precision="12" saw-sql:scale="0" saw-sql:tableHeading="" saw-sql:type="integer" type="xsd:int"/>
<xsd:element maxOccurs="1" minOccurs="0" name="Column1" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:columnHeading="ISBN" saw-sql:displayFormula=""Bibliographic Details"."ISBN"" saw-sql:length="255" saw-sql:precision="255" saw-sql:scale="0" saw-sql:tableHeading="Bibliographic Details" saw-sql:type="varchar" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="Column2" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:columnHeading="ISSN" saw-sql:displayFormula=""Bibliographic Details"."ISSN"" saw-sql:length="255" saw-sql:precision="255" saw-sql:scale="0" saw-sql:tableHeading="Bibliographic Details" saw-sql:type="varchar" type="xsd:string"/>
<xsd:element maxOccurs="1" minOccurs="0" name="Column3" saw-sql:aggregationRule="none" saw-sql:aggregationType="nonAgg" saw-sql:columnHeading="Publication Date" saw-sql:displayFormula=""Bibliographic Details"."Publication Date"" saw-sql:length="255" saw-sql:precision="255" saw-sql:scale="0" saw-sql:tableHeading="Bibliographic Details" saw-sql:type="varchar" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
<Row>
<Column0>0</Column0>
<Column1>55555555 444444445</Column1>
<Column3>[2019]</Column3>
</Row>
<Row>
<Column0>0</Column0>
<Column1>555555555</Column1>
<Column3>©2009.</Column3>
</Row>
我正在使用PHP的SimpleXML来解析这些数据,但是很难访问位于xsd:element下的非默认名称空间中的列标题。saw-sql:columnHeading=“Publication Date”,因为该列可以是动态的,并不总是“Publication Date”。因此,我希望取出saw-sql[@columnHeading]的值。
我尝试了所有的方式来注册Xpath的名称空间,使用attributes()等,我得到的最接近的是:
$ResponseXml->registerXPathNamespace('xsd','http://www.w3.org/2001/XMLSchema');
$elements = $ResponseXml->xpath('//xsd:element[@minOccurs]');
这实际上为我提供了默认的命名空间属性,但我需要saw-sql的属性,以及相同的方法:
$ResponseXml->registerXPathNamespace('saw-sql','urn:saw-sql');
$elements = $ResponseXml->xpath('//saw-sql:element[@columnHeading]');
却没有任何结果。
2条答案
按热度按时间oewdyzsn1#
XPath
//saw-sql:element[@columnHeading]
正在查找名为element
的元素(在saw-sql名称空间中),这些元素具有名为columnHeading
的属性(没有名称空间),但是元素名称实际上在xsd
名称空间中,而属性 * 在saw-sql
名称空间中。所以我相信你想要的是:
sczxawaw2#
例如,您可以使用DOMDocument而不是SimpleXML来解析它
收益率