0

我正在使用 SimpleXML 解析 XML 有效负载。我正在使用 XSL 转换来删除命名空间信息等;只得到一个干净的 xml 数据包(顺便说一下,我最初使用 preg_replace 来获得同样的东西;但由于其他原因,我们在将来会以这种方式做更多的工作,所以已经转移到 xslt。)

该过程似乎运行良好;除了某些元素。在以下输出中缺少街道名称属性。

如果我获取 XML 代码并将其粘贴到http://xmlgrid.net/ (或任何其他),则有效负载按预期解析。我真的很困惑。帮助?

array(10) {
  ["BuildingName"]=>
  object(SimpleXMLElement)#28 (0) {
  }
  ["FloorNo"]=>
  object(SimpleXMLElement)#30 (0) {
  }
  ["UnitNo"]=>
  object(SimpleXMLElement)#31 (0) {    
  }
  ["LotNo"]=>
  string(3) "128"
  ["StreetNo"]=>
  string(3) "167"
  ["Street"]=>                      //  Missing Street Type
  string(4) "PITT"                  //  Should be Street in here
  ["City"]=>
   string(7) "REDFERN"
  ["State"]=>
  object(SimpleXMLElement)#32 (1) {
    ["@attributes"]=>
    array(1) {
      ["Name"]=>
      string(3) "NSW"
   }
  }
  ["Postcode"]=>
  string(4) "2015"
  ["Country"]=>
  string(9) "Australia"
} 

我在几个元素上遇到了困难。示例代码如下:

// Get the XML Packet and the transform

            $data = file_get_contents("/mnt/tmp/test.xml");
    $tsf = file_get_contents("/mnt/tmp/tsf.xslt");
    // Load the XML data source
    $xml= simplexml_load_file('/mnt/tmp/test.xml');
    // Load the XML stylesheet
    $xsl = simplexml_load_file('/mnt/tmp/tsf.xslt');
    // create an xslt processor instance
    $proc = new XSLTProcessor;
    // import the xsl stylesheet into the xslt processor
    $proc->importStyleSheet($xsl);
    // Transform and output the xml data source
    $m =  $proc->transformToXML($xml);

    var_dump ($m);
    $z = new SimpleXMLElement($m);

    $f= (array)$z->Body->ValuationTransaction->Message->ValuationType->FullRegistered->RealEstate->Location->Address;

    var_dump ($f);

XSLT 如下:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>    

 <!-- keep comments -->
 <xsl:template match="comment()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
    <!-- remove element prefix -->
    <xsl:element name="{local-name()}">
      <!-- process attributes -->
      <xsl:for-each select="@*">
        <!-- remove attribute prefix -->
        <xsl:attribute name="{local-name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

XML 有效载荷如下:

<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><l:ValuationTransaction xmlns:l="http://www.lixi.org.au/schema/cal1.3/ValuationTransaction" ProductionData="Yes"><l:RevisionNumber LIXIVersion="1.0" UserType="Lender" /><l:Identifier Description="VMS" Type="ThirdPartyAssigned" UniqueID="9528465" /><l:Date>2013-10-18</l:Date><l:Time>13:52:24</l:Time><l:Comment></l:Comment><l:Publisher><l:RelatedEntityRef RelatedID="vms01" /></l:Publisher><l:Message><l:Identifier Description="VMS" Type="ThirdPartyAssigned" UniqueID="9528465" /><l:MessageRelatesTo><l:Identifier Type="LenderAssigned" UniqueID="1234567892" /></l:MessageRelatesTo><l:MessageBody Type="Information"><l:Status Name="Assigned">
        <l:Date>2013-10-18</l:Date>
        <l:Time>10:34:00
    </l:Time>
     </l:Status></l:MessageBody><l:ValuationType>
     <l:Identifier Description="Customer Name" Type="LenderAssigned" UniqueID="Mister Smith" />
     <l:Identifier Description="Loan Reference" Type="LenderAssigned" UniqueID="A-4000" />
     <l:Identifier Description="Customer Number" Type="LenderAssigned" UniqueID="" />
     <l:Identifier Description="Business Unit" Type="LenderAssigned" UniqueID="8023" />
     <l:Identifier Description="Valuation ID" Type="BrokerAssigned" UniqueID="1ZJX12380Y" />
     <l:Identifier Description="Replacement Valuation ID" Type="ThirdPartyAssigned" UniqueID="" />
     <l:Identifier Description="Replacement Valuation Type" Type="ThirdPartyAssigned" UniqueID="" />
     <l:Identifier Description="Valuer Reference Number" Type="ValuerAssigned" UniqueID="80145553" />
     <l:FullRegistered OtherReasonDescription="Other" PropertyTypeDescription="Fully Detached House" ReasonFor="Other" ValSubType="Standard">
        <l:RealEstate Status="Established">
           <l:Identifier Type="BrokerAssigned" UniqueID="80HLKWII" />
           <l:Residential Type="FullyDetachedHouse" />
           <l:EstimatedValue Amount="1000000" EstimateBasis="CustomerEstimate">
              <l:Date>2013-10-17</l:Date>
           </l:EstimatedValue>
           <l:Location>
              <l:Address>
                 <l:BuildingName />
                 <l:FloorNo />
                 <l:UnitNo />
                 <l:LotNo>128</l:LotNo>
                 <l:StreetNo>167</l:StreetNo>
                 <l:Street OtherTypeDescription="" Type="Street">PITT</l:Street>
                 <l:City>REDFERN</l:City>
                 <l:State Name="NSW" />
                 <l:Postcode>2015</l:Postcode>
                 <l:Country>Australia</l:Country>
              </l:Address>
           </l:Location>
        </l:RealEstate>
        <l:RequestDate>
           <l:Date>2013-10-17</l:Date>
        </l:RequestDate>
        <l:DetailedComment>
           <l:RelatedEntityRef RelatedID="80HLKWII" />
           <l:Comment />
        </l:DetailedComment>
        <l:FeeSegment>
           <l:Fee Amount="242.00" Class="Valuer">
              <l:Identifier UniqueID="CurrentFee" />
           </l:Fee>
           <l:Fee Amount="142.00" Class="Valuer">
              <l:Identifier UniqueID="InitialAllocationFee" />
           </l:Fee>
        </l:FeeSegment>
     </l:FullRegistered>
  </l:ValuationType></l:Message><l:RelatedPartySegment><l:RelatedParty RelPartyDescription="Sandstone VMS" RelPartyType="ServiceCentre"><l:Identifier Type="Sequential" UniqueID="vms01" /></l:RelatedParty><l:RelatedParty RelPartyDescription="Suncorp Metway" RelPartyType="Lender">
     <l:Identifier Type="Sequential" UniqueID="lender01" />
  </l:RelatedParty><l:RelatedParty RelPartyType="ValuationFirm">
     <l:Identifier Type="Sequential" UniqueID="valFirm01" />
     <l:Identifier Description="Reference ID" Type="ThirdPartyAssigned" UniqueID="2038" />
     <l:CompanyName BusinessName="TEST" />
     <l:Address>
        <l:NonStdAddress>PO Box 1444 </l:NonStdAddress>
        <l:City>Abbotsford</l:City>
        <l:State Name="NSW" />
        <l:Postcode>2216</l:Postcode>
        <l:Country>Australia</l:Country>
     </l:Address>
     <l:WorkPhone>
        <l:Phone>
           <l:FixedPhone>1300790000</l:FixedPhone>
        </l:Phone>
     </l:WorkPhone>
     <l:WorkPhone>
        <l:Phone>
           <l:Fax>1300 793 000</l:Fax>
        </l:Phone>
     </l:WorkPhone>
     <l:Email>sandstone@mtest.com</l:Email>
  </l:RelatedParty><l:RelatedParty RelPartyType="Valuer">
     <l:Identifier Type="Sequential" UniqueID="valuer01" />
     <l:PersonName>
        <l:FirstName>Tasso</l:FirstName>
        <l:Surname>Balo</l:Surname>
     </l:PersonName>
     <l:Email>sandstone@mvsva.net</l:Email>
     <l:ProfessionalInfrastructure>
        <l:Accreditation AccreditationID="VAL010539" Type="Licence">
           <l:Identifier UniqueID="VAL010539" />
        </l:Accreditation>
     </l:ProfessionalInfrastructure>
  </l:RelatedParty><l:RelatedParty RelPartyType="AuthorisingValuer">
     <l:Identifier Type="Sequential" UniqueID="authValuer01" />
     <l:PersonName>
        <l:FirstName>Tasso</l:FirstName>
        <l:Surname>Balom</l:Surname>
     </l:PersonName>
     <l:Email>sandstone@mvsvaluers.net</l:Email>
     <l:ProfessionalInfrastructure>
        <l:Accreditation AccreditationID="VAL010539" Type="Licence">
           <l:Identifier UniqueID="VAL010539" />
        </l:Accreditation>
     </l:ProfessionalInfrastructure>
  </l:RelatedParty><l:RelatedParty RelPartyDescription="Dean llings" RelPartyType="Instructor">
     <l:Identifier Description="Contact ID" UniqueID="Dean oll" />
     <l:PersonName>
        <l:FirstName>Dean</l:FirstName>
        <l:Surname>llings</l:Surname>
     </l:PersonName>
     <l:WorkPhone>
        <l:Phone>
           <l:FixedPhone>0355839</l:FixedPhone>
        </l:Phone>
     </l:WorkPhone>
     <l:Email>SunlinkValuations@suncorp.biz</l:Email>
  </l:RelatedParty><l:RelatedParty RelPartyType="Vendor">
     <l:Identifier UniqueID="MJVR4IBY" />
     <l:PersonName IsPreferredContact="MostPreferred">
        <l:FirstName>Angelo</l:FirstName>
        <l:Surname>Gou</l:Surname>
     </l:PersonName>
     <l:HomePhone>
        <l:Phone>
           <l:FixedPhone>082909</l:FixedPhone>
        </l:Phone>
     </l:HomePhone>
     <l:HomePhone PreferredContactMethod="Yes">
        <l:Phone>
           <l:Mobile>082909</l:Mobile>
        </l:Phone>
     </l:HomePhone>
  </l:RelatedParty></l:RelatedPartySegment></l:ValuationTransaction></soapenv:Body></soapenv:Envelope>
4

1 回答 1

1

这可能不是您要寻找的答案,但如果您只是剥离名称空间,因为您不知道如何将它们与 SimpleXML 一起使用,请查看->childrenand->attributes方法。

此外,正如手册反复声明的那样,var_dump等将不会给出 SimpleXML 元素的全貌,因为它是动态 API,而不是“普通”PHP 对象。您也不会通过转换为(array). (特别是,具有字符串内容和属性的元素,如Street您的示例中所示,仅显示其文本内容。)

你需要知道的事情是:

  • 命名空间可以通过它们的前缀来引用,这可能因文档而异,或者它们的 URI,这不会。
  • 选择一个命名空间->children()使所有后续使用都->引用该命名空间内的子项,直到您再次调用->children()或再次切换->attributes()
  • 要“切换到”默认或未定义的命名空间,您可以传递NULL->children()->attributes()
  • 没有前缀的属性总是被认为没有命名空间,即使文档为没有前缀的元素声明了默认命名空间。这是 XML 命名空间规范的奇怪之处,而不是 SimpleXML。

这是一个示例(此处有一个现场演示),说明如何将所有这些放在一起以呼应元素的Type属性:Street

// Define constants to refer to namespaces without relying on a particular prefix
define('NS_SOAP', 'http://schemas.xmlsoap.org/soap/envelope/');
define('NS_VALUATION', 'http://www.lixi.org.au/schema/cal1.3/ValuationTransaction');

echo $xml
    // Body element is in the SOAP namespace
    ->children(NS_SOAP)->Body
    // Switch to the inner namespace
    ->children(NS_VALUATION)
    // Traverse to the element we want
    ->ValuationTransaction
    ->Message->ValuationType->FullRegistered->RealEstate->Location->Address
    ->Street
    // The attribute has no prefix, and is therefore not in any namespace, so we need to switch to the NULL namespace
    ->attributes(NULL)->Type;
于 2013-10-19T21:11:20.820 回答