You are here:  » dynamic attributes


dynamic attributes

Submitted by jorrit on Tue, 2013-08-20 13:31 in

Hello,

My XML source has attributes in tags, like . I know that I can retrieve this data as $record['person-name'] but the attributes differ often and my code does not know all possible attributenames. Can I access the attributes with something like $record['person'][0] or do I need to extract the names and values programmatically?

Thanks for your support.

Jorrit Steetskamp

Submitted by support on Tue, 2013-08-20 13:54

Hello Jorrit, and welcome to the forum!

Here's a simple way to "extract" attributes, which are presented as, like you say (although always UPPER-CASE for XML) as $record["ELEMENT-ATTRIBUTE"] into separate child arrays of $record. So if you have

$record["NAME-AGE"]
and
$record["NAME-GENDER"]

This method will convert those to

$record["NAME"]["AGE"]
$record["NAME"]["GENDER"]

To do this, modify your myRecordHandler() function as follows:

  function myRecordHandler($record)
  {
    foreach($record as $k => $v)
    {
      if (strpos($k,"-"))
      {
        $parts = explode("-",$k);
        $record[$parts[0]][$parts[1]] = $v;
        unset($record[$k]);
      }
    }
    // rest of record handler
    // so to print the attributes of $record["NAME"] use
    if (is_array($record["NAME"]))
    {
      foreach($record["NAME"] as $k => $v)
      {
        print "<p>Attribute: ".$k." Value: ".$v."</p>";
      }
    }
  }

Hope this helps!

Cheers,
David
--
MagicParser.com

Submitted by jorrit on Wed, 2013-08-21 07:45

Hi David,

Thanks for your answer to my question. It surely helps!

My XML file is a little more complicated than I mentioned in my first post. Here is an example:

<?xml version="1.0" encoding="UTF-8"?>
<CompactData xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message" xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common" xmlns:compact="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/compact" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:data="urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=EUROSTAT:sts_inprgr_m_DSD:compact" xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message SDMXMessage.xsd urn:sdmx:org.sdmx.infomodel.keyfamily.KeyFamily=EUROSTAT:sts_inprgr_m_DSD:compact EUROSTAT_sts_inprgr_m_Compact.xsd http://www.SDMX.org/resources/SDMXML/schemas/v2_0/compact SDMXCompactData.xsd">
<Header>
<ID>sts_inprgr_m</ID>
<Test>false</Test>
<Name xml:lang="en">sts_inprgr_m</Name>
<Prepared>2013-08-17T02:46:39</Prepared>
<Sender id="EUROSTAT">
<Name xml:lang="en">EUROSTAT</Name>
</Sender>
<Receiver id="XML">
<Name xml:lang="en">SDMX-ML File</Name>
</Receiver>
<DataSetID>sts_inprgr_m</DataSetID>
<Extracted>2013-08-17T02:46:39</Extracted>
</Header>
<data:DataSet>
<data:Series FREQ="M" indic_bt="PROD" nace_r2="B" s_adj="SA" unit="PCH_PRE" geo="AT" TIME_FORMAT="P1M">
<data:Obs TIME_PERIOD="2012-01" OBS_VALUE="-15.2" />
<data:Obs TIME_PERIOD="2012-02" OBS_VALUE="6.7" />
<data:Obs TIME_PERIOD="2012-03" OBS_VALUE="2.4" />
<data:Obs TIME_PERIOD="2012-04" OBS_VALUE="1.1" />
<data:Obs TIME_PERIOD="2012-05" OBS_VALUE="-2.5" />
<data:Obs TIME_PERIOD="2012-06" OBS_VALUE="-3.8" />
<data:Obs TIME_PERIOD="2012-07" OBS_VALUE="6.1" />
<data:Obs TIME_PERIOD="2012-08" OBS_VALUE="3.6" />
<data:Obs TIME_PERIOD="2012-09" OBS_VALUE="-0.1" />
<data:Obs TIME_PERIOD="2012-10" OBS_VALUE="-5.2" />
<data:Obs TIME_PERIOD="2012-11" OBS_VALUE="4.1" />
<data:Obs TIME_PERIOD="2012-12" OBS_VALUE="-3.4" />
<data:Obs TIME_PERIOD="2013-01" OBS_VALUE="-0.2" />
<data:Obs TIME_PERIOD="2013-02" OBS_VALUE="-22.4" />
<data:Obs TIME_PERIOD="2013-03" OBS_VALUE="13.5" />
<data:Obs TIME_PERIOD="2013-04" OBS_VALUE="5.6" />
<data:Obs TIME_PERIOD="2013-05" OBS_VALUE="2.5" OBS_STATUS="p" />
</data:Series>
<data:Series FREQ="M" indic_bt="PROD" nace_r2="B" s_adj="SA" unit="PCH_PRE" geo="BA" TIME_FORMAT="P1M">
<data:Obs TIME_PERIOD="2006-02" OBS_VALUE="2.7" />
<data:Obs TIME_PERIOD="2006-03" OBS_VALUE="-4.6" />
<data:Obs TIME_PERIOD="2006-04" OBS_VALUE="4.2" />
<data:Obs TIME_PERIOD="2006-05" OBS_VALUE="-4.2" />
<data:Obs TIME_PERIOD="2006-06" OBS_VALUE="1.7" />
<data:Obs TIME_PERIOD="2006-07" OBS_VALUE="5.0" />
...etc...

I want attribute values from both OBS and SERIES elements. It is fine to JOIN them. Can you help me?

Secondly I would be very pleased if you could tell me what Format String it needs. Without a format string I get a timeout. The file is 120MB large.

Thanks in advance.

Regards,
Jorrit Steetskamp

Submitted by support on Wed, 2013-08-21 09:11

Hello Jorrit,

This style of XML may be easier to process with a version of MagicParser.php that I have which provides access to parent elements, so you can parse at the following level (Format String)

xml|COMPACTDATA/DATA:DATASET/DATA:SERIES/DATA:OBS/

...and still have access to the parent DATA:SERIES attributes.

I will email you this version to try in just a moment together with an example...

Cheers,
David
--
MagicParser.com

Submitted by jorrit on Wed, 2013-08-21 11:20

Thank you very much for your fast and helpful answers!