mohit sharma
mohit sharma

Reputation: 1070

How to parse this XML(XBRL) using Php

I am new to PHP and I need to parse an XML file, make some changes in it and write it.

The file looks like this:

<xbrli:xbrl xsi:schemaLocation="http://xbrl.org/2006/xbrldi http://www.xbrl.org/2006/xbrldi-2006.xsd"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xmlns:odrpt.0001.list.req.02.00="http://sbr.gov.au/rprt/ato/odrpt/odrpt.0001.list.request.02.00.report"
                    xmlns:ref="http://www.xbrl.org/2006/ref"
                    xmlns:dtyp.02.24="http://sbr.gov.au/fdtn/sbr.02.24.dtyp"
                    xmlns:pyid.02.10="http://sbr.gov.au/icls/py/pyid/pyid.02.10.data"
                    xmlns:emsup.02.08="http://sbr.gov.au/icls/em/emsup/emsup.02.08.data"
                    xmlns:emsup.02.20="http://sbr.gov.au/icls/em/emsup/emsup.02.20.data"
                    xmlns:xbrldt="http://xbrl.org/2005/xbrldt"
                    xmlns:SqNumDim.02.01_typedelement="http://sbr.gov.au/dims/SqNumDim.02.01.dims"
                    xmlns:pyde.02.20="http://sbr.gov.au/icls/py/pyde/pyde.02.20.data"
                    xmlns:dtyp.02.00="http://sbr.gov.au/fdtn/sbr.02.00.dtyp"
                    xmlns:pyid.02.00="http://sbr.gov.au/icls/py/pyid/pyid.02.00.data"
                    xmlns:link="http://www.xbrl.org/2003/linkbase"
                    xmlns:xlink="http://www.w3.org/1999/xlink"
                    xmlns:odrpt.0001.prv.02.00="http://sbr.gov.au/rprt/ato/odrpt/odrpt.0001.private.02.00.module"
                    xmlns:iso4217="http://www.xbrl.org/2003/iso4217"
                    xmlns:pyde.02.00="http://sbr.gov.au/icls/py/pyde/pyde.02.00.data"
                    xmlns:pyde.02.08="http://sbr.gov.au/icls/py/pyde/pyde.02.08.data"
                    xmlns:tech.01.02="http://sbr.gov.au/fdtn/sbr.01.02.tech"
                    xmlns:xbrldi="http://xbrl.org/2006/xbrldi"
                    xmlns:RprtPyType.02.13="http://sbr.gov.au/dims/RprtPyType.02.13.dims"
                    xmlns:tech.01.03="http://sbr.gov.au/fdtn/sbr.01.03.tech"
                    xmlns:xbrli="http://www.xbrl.org/2003/instance"
                    xmlns:pyin.02.02="http://sbr.gov.au/icls/py/pyin/pyin.02.02.data"
                    xmlns:pyde.02.12="http://sbr.gov.au/icls/py/pyde/pyde.02.12.data"
                    xmlns:pyid.02.03="http://sbr.gov.au/icls/py/pyid/pyid.02.03.data">
                    <link:schemaRef xlink:type="simple" xlink:href="http://sbr.gov.au/taxonomy/sbr_au_reports/ato/usmat/usmat_0001/usmat.0001.list.request.02.00.report.xsd"/>
                    <xbrli:context id="Context_Duration_ReportingParty">
                        <xbrli:entity>
                            <xbrli:identifier scheme="http://www.abr.gov.au/abn">111111111</xbrli:identifier>
                            <xbrli:segment>
                                <xbrldi:explicitMember dimension="RprtPyType.02.13:ReportPartyTypeDimension">RprtPyType.02.13:ReportingParty</xbrldi:explicitMember>
                            </xbrli:segment>
                        </xbrli:entity>
                        <xbrli:period>
                            <xbrli:startDate>2014-06-01</xbrli:startDate>
                            <xbrli:endDate>2014-06-01</xbrli:endDate>
                        </xbrli:period>
                    </xbrli:context>
                    <xbrli:context id="Context_Duration_SuperFundMember">
                        <xbrli:entity>
                            <xbrli:identifier scheme="http://www.ato.gov.au/tfn">11111111</xbrli:identifier>
                            <xbrli:segment>
                                <xbrldi:explicitMember dimension="RprtPyType.02.13:ReportPartyTypeDimension">RprtPyType.02.13:SuperFundMember</xbrldi:explicitMember>
                            </xbrli:segment>
                        </xbrli:entity>
                        <xbrli:period>
                            <xbrli:startDate>2015-06-01</xbrli:startDate>
                            <xbrli:endDate>2014-06-01</xbrli:endDate>
                        </xbrli:period>
                    </xbrli:context>
                    <pyde.02.00:OrganisationNameDetails.OrganisationalName.Text contextRef="Context_Duration_ReportingParty">CCCorp</pyde.02.00:OrganisationNameDetails.OrganisationalName.Text>
                    <pyde.02.00:PersonNameDetails.FamilyName.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:PersonNameDetails.FamilyName.Text>
                    <pyde.02.00:PersonNameDetails.GivenName.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:PersonNameDetails.GivenName.Text>
                    <pyde.02.00:PersonNameDetails.OtherGivenName.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:PersonNameDetails.OtherGivenName.Text>
                    <pyde.02.12:PersonDemographicDetails.Birth.DayofMonth contextRef="Context_Duration_SuperFundMember">---18</pyde.02.12:PersonDemographicDetails.Birth.DayofMonth>
                    <pyde.02.12:PersonDemographicDetails.Birth.Month contextRef="Context_Duration_SuperFundMember">--12</pyde.02.12:PersonDemographicDetails.Birth.Month>
                    <pyde.02.12:PersonDemographicDetails.Birth.Year contextRef="Context_Duration_SuperFundMember">1960</pyde.02.12:PersonDemographicDetails.Birth.Year>
                    <pyid.02.10:Identifiers.SuperannuationMemberAccount.Identifier contextRef="Context_Duration_SuperFundMember">true</pyid.02.10:Identifiers.SuperannuationMemberAccount.Identifier>
                    <pyde.02.00:AddressDetails.Line1.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:AddressDetails.Line1.Text>
                    <pyde.02.00:AddressDetails.Line2.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:AddressDetails.Line2.Text>
                    <pyde.02.00:AddressDetails.LocalityName.Text contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:AddressDetails.LocalityName.Text>
                    <pyde.02.00:AddressDetails.Postcode.Text contextRef="Context_Duration_SuperFundMember">3350</pyde.02.00:AddressDetails.Postcode.Text>
                    <pyde.02.00:AddressDetails.StateOrTerritory.Code contextRef="Context_Duration_SuperFundMember">CC</pyde.02.00:AddressDetails.StateOrTerritory.Code>
                    <pyde.02.08:AddressDetails.Country.Code contextRef="Context_Duration_SuperFundMember">au</pyde.02.08:AddressDetails.Country.Code>
                    <emsup.02.08:SuperannuationFundDetails.UniqueSuperannuationIdentifier.Identifier contextRef="Context_Duration_SuperFundMember">abc1234ab</emsup.02.08:SuperannuationFundDetails.UniqueSuperannuationIdentifier.Identifier>
                </xbrli:xbrl>

You can view it here: https://codebeautify.org/xmlviewer/cbfe5ebd

I have tried DOM parser using some example, But i can't parse fields like :

<pyde.02.00:AddressDetails.Postcode.Text>

Let me know if any other information is required.

Upvotes: 1

Views: 2398

Answers (2)

Nigel Ren
Nigel Ren

Reputation: 57131

If you load the data using something like simplexml_load_string then the following will do the job...

    $xml = simplexml_load_string( '
            <xbrli:xbrl xsi:schemaLocation="http://xbrl.org/2006/xbrldi http://www.xbrl.org/2006/xbrldi-2006.xsd"
                       ... 
            </xbrli:xbrl>
');


foreach($xml->xpath('//pyde.02.00:AddressDetails.Postcode.Text') as $postCode) {
    print_r($postCode);
}

You will need to add in the rest of the XML document, but this is just to show the main code as opposed to re-listing the whole xml. The main thing is the XPath, which you can see has the namespace (pyde.02.00) as well as the elements name to locate the particular bit your after. You should then be able to process these elements with your own code.

Edit: To modify values, you need to change the corresponding part of the element. So above prints out...

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [contextRef] => Context_Duration_SuperFundMember
        )

    [0] => 3350
)

So if in the code you do

$postCode = $xml->xpath('//pyde.02.00:AddressDetails.Postcode.Text')[0];
$postCode[0] = "3351";

This results in the element now being

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [contextRef] => Context_Duration_SuperFundMember
        )

    [0] => 3351
)

If you want to save this to a file, then

$xml->asXml( $fileName );

Upvotes: 1

mohit sharma
mohit sharma

Reputation: 1070

Use Following code:

       $xmldoc = new DOMDocument();
            $xmldoc->load("myFile.xml");
            $xpath = new DOMXPath($xmldoc);
            $xpath->registerNamespace("xbrli",  "http://www.xbrl.org/2003/instance");
            $xpath->registerNamespace("pyde.02.00", "http://sbr.gov.au/icls/py/pyde/pyde.02.00.data");

            $organizationNameList = $xpath->query("/xbrli:xbrl/pyde.02.00:OrganisationNameDetails.OrganisationalName.Text[@contextRef='    Context_Duration_ReportingParty']");
            if($organizationNameList->length != 0){
                $orgNode = $organizationNameList->item(0);
                $orgName = $organizationNameList->length === 1 ? $organizationNameList->item(0)->nodeValue : null;
                echo $orgName;
            }

Using following code I am Able to read a value using specific query

Upvotes: 1

Related Questions