我正在使用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 Payload如下:
<?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>
答案 0 :(得分:1)
这可能不是您正在寻找的答案,但如果您因为不知道如何在SimpleXML中使用它们而剥离名称空间,请查看->children
和->attributes
方法。
此外,正如手册重复声明的那样,var_dump
等不会提供SimpleXML元素的完整图片,因为它是动态API,而不是“普通”PHP对象。通过施放到(array)
,你也不会获得多少收益。 (特别是,具有字符串内容和属性的元素,例如示例中的Street
,仅显示其文本内容。)
你需要知道的事情是:
->children()
的命名空间后,->
的所有后续使用都会引用该命名空间中的子项,直到您再次切换到->children()
或->attributes()
NULL
传递给->children()
或->attributes()
。以下是如何将所有内容放在一起以回显Type
元素的Street
属性的示例(live demo here):
// 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;