如何使用 PHP 和 SimpleXML 解析带有命名空间的 SOAP XML 响应

发布于 2024-12-21 08:23:44 字数 13108 浏览 1 评论 0原文

我正在使用 CURL 向 Mondrian 发送 SOAP 请求。这是使用 CURL 的 PHP 代码:

$poststring =
    '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-        ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
    <Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
    <Command>
    <Statement>
    select {[Measures].[Unit Sales]} on columns from Sales
    </Statement>
    </Command>
    <Properties>
    <PropertyList>
    <Catalog>FoodMart</Catalog>
    <DataSourceInfo>Provider=Mondrian;DataSource=MondrianFoodMart;</DataSourceInfo>
    <Format>Multidimensional</Format>
    <AxisFormat>TupleFormat</AxisFormat>
    </PropertyList>
    </Properties>
    </Execute>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/mondrian/xmla');
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststring);
curl_setopt_array($ch, $this->_curlOptions);        
$_rawResult = curl_exec($ch);

curl_close($ch);

我从 Mondrian 收到的 SOAP 响应是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP- ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" >
<SOAP-ENV:Header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cxmla:ExecuteResponse xmlns:cxmla="urn:schemas-microsoft-com:xml-analysis">
<cxmla:return>
<root xmlns="urn:schemas-microsoft-com:xml-analysis:mddataset"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:EX="urn:schemas-microsoft-com:xml-analysis:exception">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:schemas-microsoft-com:xml-analysis:mddataset" xmlns="urn:schemas-microsoft-com:xml-analysis:mddataset" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sql="urn:schemas-microsoft-com:xml-sql" elementFormDefault="qualified">
<xsd:complexType name="MemberType">
<xsd:sequence>
<xsd:element name="UName" type="xsd:string"/>
<xsd:element name="Caption" type="xsd:string"/>
<xsd:element name="LName" type="xsd:string"/>
<xsd:element name="LNum" type="xsd:unsignedInt"/>
<xsd:element name="DisplayInfo" type="xsd:unsignedInt"/>
        <xsd:sequence maxOccurs="unbounded" minOccurs="0">
          <xsd:any processContents="lax" maxOccurs="unbounded"/>
        </xsd:sequence>
      </xsd:sequence>
      <xsd:attribute name="Hierarchy" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="PropType">
      <xsd:attribute name="name" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="TupleType">
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="Member" type="MemberType"/>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="MembersType">
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="Member" type="MemberType"/>
      </xsd:sequence>
      <xsd:attribute name="Hierarchy" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="TuplesType">
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="Tuple" type="TupleType"/>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CrossProductType">
      <xsd:sequence>
        <xsd:choice minOccurs="0" maxOccurs="unbounded">
          <xsd:element name="Members" type="MembersType"/>
          <xsd:element name="Tuples" type="TuplesType"/>
        </xsd:choice>
      </xsd:sequence>
      <xsd:attribute name="Size" type="xsd:unsignedInt"/>
    </xsd:complexType>
    <xsd:complexType name="OlapInfo">
      <xsd:sequence>
        <xsd:element name="CubeInfo">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Cube" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="CubeName" type="xsd:string"/>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="AxesInfo">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="AxisInfo" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="HierarchyInfo" minOccurs="0" maxOccurs="unbounded">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:sequence maxOccurs="unbounded">
                            <xsd:element name="UName" type="PropType"/>
                            <xsd:element name="Caption" type="PropType"/>
                            <xsd:element name="LName" type="PropType"/>
                            <xsd:element name="LNum" type="PropType"/>
                            <xsd:element name="DisplayInfo" type="PropType" minOccurs="0" maxOccurs="unbounded"/>
                          </xsd:sequence>
                          <xsd:sequence>
                            <xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
                          </xsd:sequence>
                        </xsd:sequence>
                        <xsd:attribute name="name" type="xsd:string" use="required"/>
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string"/>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="CellInfo">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                <xsd:choice>
                  <xsd:element name="Value" type="PropType"/>
                  <xsd:element name="FmtValue" type="PropType"/>
                  <xsd:element name="BackColor" type="PropType"/>
                  <xsd:element name="ForeColor" type="PropType"/>
                  <xsd:element name="FontName" type="PropType"/>
                  <xsd:element name="FontSize" type="PropType"/>
                  <xsd:element name="FontFlags" type="PropType"/>
                  <xsd:element name="FormatString" type="PropType"/>
                  <xsd:element name="NonEmptyBehavior" type="PropType"/>
                  <xsd:element name="SolveOrder" type="PropType"/>
                  <xsd:element name="Updateable" type="PropType"/>
                  <xsd:element name="Visible" type="PropType"/>
                  <xsd:element name="Expression" type="PropType"/>
                </xsd:choice>
              </xsd:sequence>
              <xsd:sequence maxOccurs="unbounded" minOccurs="0">
                <xsd:any processContents="lax" maxOccurs="unbounded"/>
              </xsd:sequence>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Axes">
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="Axis">
          <xsd:complexType>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
              <xsd:element name="CrossProduct" type="CrossProductType"/>
              <xsd:element name="Tuples" type="TuplesType"/>
              <xsd:element name="Members" type="MembersType"/>
            </xsd:choice>
            <xsd:attribute name="name" type="xsd:string"/>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CellData">
      <xsd:sequence>
        <xsd:element name="Cell" minOccurs="0" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence maxOccurs="unbounded">
              <xsd:choice>
                <xsd:element name="Value"/>
                <xsd:element name="FmtValue" type="xsd:string"/>
                <xsd:element name="BackColor" type="xsd:unsignedInt"/>
                <xsd:element name="ForeColor" type="xsd:unsignedInt"/>
                <xsd:element name="FontName" type="xsd:string"/>
                <xsd:element name="FontSize" type="xsd:unsignedShort"/>
                <xsd:element name="FontFlags" type="xsd:unsignedInt"/>
                <xsd:element name="FormatString" type="xsd:string"/>
                <xsd:element name="NonEmptyBehavior" type="xsd:unsignedShort"/>
                <xsd:element name="SolveOrder" type="xsd:unsignedInt"/>
                <xsd:element name="Updateable" type="xsd:unsignedInt"/>
                <xsd:element name="Visible" type="xsd:unsignedInt"/>
                <xsd:element name="Expression" type="xsd:string"/>
              </xsd:choice>
            </xsd:sequence>
            <xsd:attribute name="CellOrdinal" type="xsd:unsignedInt" use="required"/>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="root">
      <xsd:complexType>
        <xsd:sequence maxOccurs="unbounded">
          <xsd:element name="OlapInfo" type="OlapInfo"/>
          <xsd:element name="Axes" type="Axes"/>
          <xsd:element name="CellData" type="CellData"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <OlapInfo>
    <CubeInfo>
      <Cube>
        <CubeName>Sales</CubeName>
      </Cube>
    </CubeInfo>
    <AxesInfo>
      <AxisInfo name="Axis0">
        <HierarchyInfo name="Measures">
          <UName name="[Measures].[MEMBER_UNIQUE_NAME]"/>
          <Caption name="[Measures].[MEMBER_CAPTION]"/>
          <LName name="[Measures].[LEVEL_UNIQUE_NAME]"/>
          <LNum name="[Measures].[LEVEL_NUMBER]"/>
          <DisplayInfo name="[Measures].[DISPLAY_INFO]"/>
        </HierarchyInfo>
      </AxisInfo>
    </AxesInfo>
    <CellInfo>
      <Value name="VALUE"/>
      <FmtValue name="FORMATTED_VALUE"/>
      <FormatString name="FORMAT_STRING"/>
    </CellInfo>
  </OlapInfo>
  <Axes>
    <Axis name="Axis0">
      <Tuples>
        <Tuple>
          <Member Hierarchy="Measures">
            <UName>[Measures].[Unit Sales]</UName>
            <Caption>Unit Sales</Caption>
            <LName>[Measures].[MeasuresLevel]</LName>
            <LNum>0</LNum>
            <DisplayInfo>0</DisplayInfo>
          </Member>
        </Tuple>
      </Tuples>
    </Axis>
  </Axes>
  <CellData>
    <Cell CellOrdinal="0">
      <Value xsi:type="xsd:double">266773</Value>
      <FmtValue>266,773</FmtValue>
      <FormatString>Standard</FormatString>
    </Cell>
  </CellData>
</root>
</cxmla:return>
</cxmla:ExecuteResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我的目标是提取 CellData 元素内的所有 Cell 元素。尽管这一响应仅来自一个 Cell,但进一步的答案可能来自多个 Cell。我正在考虑使用 XPath,但我可能会丢失一些与命名空间注册相关的细节:

$xml = simplexml_load_string($_rawResponse);
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('cxmla', 'urn:schemas-microsoft-com:xml-analysis');
$_res = $xml->xpath('//CellData/Cell');
var_dump($_res);

var_dump 的结果是:

array(0) {
}

而不是类似:

array(1) {
  [0]=>
  object(SimpleXMLElement)#2 (4) {
    ["@attributes"]=>
    array(1) {
      ["CellOrdinal"]=>
      string(1) "0"
    }
    ["Value"]=>
    string(4) "266773"
    ["FmtValue"]=>
    string(5) "266,773"
    ["FormatString"]=>
    string(8) "Standard"
  }
}

我做错了什么?你能指出我正确的方向吗?先感谢您。

I'm using CURL to send a SOAP request to Mondrian. This is the PHP code where CURL is being used:

$poststring =
    '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-        ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
    <Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
    <Command>
    <Statement>
    select {[Measures].[Unit Sales]} on columns from Sales
    </Statement>
    </Command>
    <Properties>
    <PropertyList>
    <Catalog>FoodMart</Catalog>
    <DataSourceInfo>Provider=Mondrian;DataSource=MondrianFoodMart;</DataSourceInfo>
    <Format>Multidimensional</Format>
    <AxisFormat>TupleFormat</AxisFormat>
    </PropertyList>
    </Properties>
    </Execute>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8080/mondrian/xmla');
curl_setopt($ch, CURLOPT_POSTFIELDS, $poststring);
curl_setopt_array($ch, $this->_curlOptions);        
$_rawResult = curl_exec($ch);

curl_close($ch);

The received SOAP response I get from Mondrian is:

<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP- ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" >
<SOAP-ENV:Header>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cxmla:ExecuteResponse xmlns:cxmla="urn:schemas-microsoft-com:xml-analysis">
<cxmla:return>
<root xmlns="urn:schemas-microsoft-com:xml-analysis:mddataset"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:EX="urn:schemas-microsoft-com:xml-analysis:exception">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:schemas-microsoft-com:xml-analysis:mddataset" xmlns="urn:schemas-microsoft-com:xml-analysis:mddataset" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sql="urn:schemas-microsoft-com:xml-sql" elementFormDefault="qualified">
<xsd:complexType name="MemberType">
<xsd:sequence>
<xsd:element name="UName" type="xsd:string"/>
<xsd:element name="Caption" type="xsd:string"/>
<xsd:element name="LName" type="xsd:string"/>
<xsd:element name="LNum" type="xsd:unsignedInt"/>
<xsd:element name="DisplayInfo" type="xsd:unsignedInt"/>
        <xsd:sequence maxOccurs="unbounded" minOccurs="0">
          <xsd:any processContents="lax" maxOccurs="unbounded"/>
        </xsd:sequence>
      </xsd:sequence>
      <xsd:attribute name="Hierarchy" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="PropType">
      <xsd:attribute name="name" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="TupleType">
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="Member" type="MemberType"/>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="MembersType">
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="Member" type="MemberType"/>
      </xsd:sequence>
      <xsd:attribute name="Hierarchy" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="TuplesType">
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="Tuple" type="TupleType"/>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CrossProductType">
      <xsd:sequence>
        <xsd:choice minOccurs="0" maxOccurs="unbounded">
          <xsd:element name="Members" type="MembersType"/>
          <xsd:element name="Tuples" type="TuplesType"/>
        </xsd:choice>
      </xsd:sequence>
      <xsd:attribute name="Size" type="xsd:unsignedInt"/>
    </xsd:complexType>
    <xsd:complexType name="OlapInfo">
      <xsd:sequence>
        <xsd:element name="CubeInfo">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="Cube" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="CubeName" type="xsd:string"/>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="AxesInfo">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="AxisInfo" maxOccurs="unbounded">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="HierarchyInfo" minOccurs="0" maxOccurs="unbounded">
                      <xsd:complexType>
                        <xsd:sequence>
                          <xsd:sequence maxOccurs="unbounded">
                            <xsd:element name="UName" type="PropType"/>
                            <xsd:element name="Caption" type="PropType"/>
                            <xsd:element name="LName" type="PropType"/>
                            <xsd:element name="LNum" type="PropType"/>
                            <xsd:element name="DisplayInfo" type="PropType" minOccurs="0" maxOccurs="unbounded"/>
                          </xsd:sequence>
                          <xsd:sequence>
                            <xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
                          </xsd:sequence>
                        </xsd:sequence>
                        <xsd:attribute name="name" type="xsd:string" use="required"/>
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                  <xsd:attribute name="name" type="xsd:string"/>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="CellInfo">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:sequence minOccurs="0" maxOccurs="unbounded">
                <xsd:choice>
                  <xsd:element name="Value" type="PropType"/>
                  <xsd:element name="FmtValue" type="PropType"/>
                  <xsd:element name="BackColor" type="PropType"/>
                  <xsd:element name="ForeColor" type="PropType"/>
                  <xsd:element name="FontName" type="PropType"/>
                  <xsd:element name="FontSize" type="PropType"/>
                  <xsd:element name="FontFlags" type="PropType"/>
                  <xsd:element name="FormatString" type="PropType"/>
                  <xsd:element name="NonEmptyBehavior" type="PropType"/>
                  <xsd:element name="SolveOrder" type="PropType"/>
                  <xsd:element name="Updateable" type="PropType"/>
                  <xsd:element name="Visible" type="PropType"/>
                  <xsd:element name="Expression" type="PropType"/>
                </xsd:choice>
              </xsd:sequence>
              <xsd:sequence maxOccurs="unbounded" minOccurs="0">
                <xsd:any processContents="lax" maxOccurs="unbounded"/>
              </xsd:sequence>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Axes">
      <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="Axis">
          <xsd:complexType>
            <xsd:choice minOccurs="0" maxOccurs="unbounded">
              <xsd:element name="CrossProduct" type="CrossProductType"/>
              <xsd:element name="Tuples" type="TuplesType"/>
              <xsd:element name="Members" type="MembersType"/>
            </xsd:choice>
            <xsd:attribute name="name" type="xsd:string"/>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CellData">
      <xsd:sequence>
        <xsd:element name="Cell" minOccurs="0" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:sequence maxOccurs="unbounded">
              <xsd:choice>
                <xsd:element name="Value"/>
                <xsd:element name="FmtValue" type="xsd:string"/>
                <xsd:element name="BackColor" type="xsd:unsignedInt"/>
                <xsd:element name="ForeColor" type="xsd:unsignedInt"/>
                <xsd:element name="FontName" type="xsd:string"/>
                <xsd:element name="FontSize" type="xsd:unsignedShort"/>
                <xsd:element name="FontFlags" type="xsd:unsignedInt"/>
                <xsd:element name="FormatString" type="xsd:string"/>
                <xsd:element name="NonEmptyBehavior" type="xsd:unsignedShort"/>
                <xsd:element name="SolveOrder" type="xsd:unsignedInt"/>
                <xsd:element name="Updateable" type="xsd:unsignedInt"/>
                <xsd:element name="Visible" type="xsd:unsignedInt"/>
                <xsd:element name="Expression" type="xsd:string"/>
              </xsd:choice>
            </xsd:sequence>
            <xsd:attribute name="CellOrdinal" type="xsd:unsignedInt" use="required"/>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="root">
      <xsd:complexType>
        <xsd:sequence maxOccurs="unbounded">
          <xsd:element name="OlapInfo" type="OlapInfo"/>
          <xsd:element name="Axes" type="Axes"/>
          <xsd:element name="CellData" type="CellData"/>
        </xsd:sequence>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <OlapInfo>
    <CubeInfo>
      <Cube>
        <CubeName>Sales</CubeName>
      </Cube>
    </CubeInfo>
    <AxesInfo>
      <AxisInfo name="Axis0">
        <HierarchyInfo name="Measures">
          <UName name="[Measures].[MEMBER_UNIQUE_NAME]"/>
          <Caption name="[Measures].[MEMBER_CAPTION]"/>
          <LName name="[Measures].[LEVEL_UNIQUE_NAME]"/>
          <LNum name="[Measures].[LEVEL_NUMBER]"/>
          <DisplayInfo name="[Measures].[DISPLAY_INFO]"/>
        </HierarchyInfo>
      </AxisInfo>
    </AxesInfo>
    <CellInfo>
      <Value name="VALUE"/>
      <FmtValue name="FORMATTED_VALUE"/>
      <FormatString name="FORMAT_STRING"/>
    </CellInfo>
  </OlapInfo>
  <Axes>
    <Axis name="Axis0">
      <Tuples>
        <Tuple>
          <Member Hierarchy="Measures">
            <UName>[Measures].[Unit Sales]</UName>
            <Caption>Unit Sales</Caption>
            <LName>[Measures].[MeasuresLevel]</LName>
            <LNum>0</LNum>
            <DisplayInfo>0</DisplayInfo>
          </Member>
        </Tuple>
      </Tuples>
    </Axis>
  </Axes>
  <CellData>
    <Cell CellOrdinal="0">
      <Value xsi:type="xsd:double">266773</Value>
      <FmtValue>266,773</FmtValue>
      <FormatString>Standard</FormatString>
    </Cell>
  </CellData>
</root>
</cxmla:return>
</cxmla:ExecuteResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

My objective is to extract all the Cell elements that come inside the CellData element. Although this response comes with only one Cell, it is possible that further answers come with more than one. I was thinking of using XPath, but I may be missing some detail related to Namespace registering:

$xml = simplexml_load_string($_rawResponse);
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('cxmla', 'urn:schemas-microsoft-com:xml-analysis');
$_res = $xml->xpath('//CellData/Cell');
var_dump($_res);

The result of var_dump is:

array(0) {
}

instead of something like:

array(1) {
  [0]=>
  object(SimpleXMLElement)#2 (4) {
    ["@attributes"]=>
    array(1) {
      ["CellOrdinal"]=>
      string(1) "0"
    }
    ["Value"]=>
    string(4) "266773"
    ["FmtValue"]=>
    string(5) "266,773"
    ["FormatString"]=>
    string(8) "Standard"
  }
}

What am I doing wrong? Could you point me in the right direction ? Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2024-12-28 08:23:45

使用 DOMDocument 您可以获得 xml 中的所有值。
尝试使用 DOMDocument 文档链接: http://php.net/manual/en/class.domdocument .php

Using DOMDocument you can get all values in xml.
Try with DOMDocument Documentation Link: http://php.net/manual/en/class.domdocument.php

坏尐絯 2024-12-28 08:23:44

这是一个命名空间问题。

元素是 元素的父元素,具有默认命名空间定义 xmlns="urn:schemas-microsoft- com:xml-analysis:mddataset" 所以 位于该命名空间中(就像它的所有后代一样)。因此,您需要注册此命名空间并为其指定一些前缀,然后在 XPath 步骤中使用该前缀。

$xml->registerXPathNamespace('md', 'urn:schemas-microsoft-com:xml-analysis:mddataset');
$_res = $xml->xpath('//md:CellData/md:Cell');

如果给定的元素名称没有前缀,XPath 查询始终搜索不在任何命名空间中的元素。

This is a namespace issue.

The <root> element, which is the parent of the <CellData> element, has a default namespace definition xmlns="urn:schemas-microsoft-com:xml-analysis:mddataset" so <CellData> is in that namespace (like all its descendants also happen to be). Therefore you need to register this namespace and give it some prefix and then use that prefix in your XPath steps.

$xml->registerXPathNamespace('md', 'urn:schemas-microsoft-com:xml-analysis:mddataset');
$_res = $xml->xpath('//md:CellData/md:Cell');

XPath queries always search for an element that is not in any namespace if the given element name is not prefixed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文