Hans Mackowiak
Hans Mackowiak

Reputation: 64

Symfony Serializer xml with complex xsd type in sequence

i got the following XSD:


<xsd:complexType name="typeADDRESS">
    <xsd:sequence>
        <xsd:element ref="NAME" minOccurs="0" maxOccurs="unbounded"/>
        <!-- # ... just to show that these aren't the only ones -->
        <xsd:sequence minOccurs="0" maxOccurs="unbounded">
            <xsd:element ref="EMAIL"/>
            <xsd:element ref="PUBLIC_KEY" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
        <!-- # ... and also some attributes later -->
    </xsd:sequence>
</xsd:complexType>

if the XML Data for the email elements looks something like this:

<EMAIL>A</EMAIL>
<PUBLIC_KEY>A1</PUBLIC_KEY>
<PUBLIC_KEY>A2</PUBLIC_KEY>
<EMAIL>B</EMAIL>
<PUBLIC_KEY>B1</PUBLIC_KEY>
<PUBLIC_KEY>B2</PUBLIC_KEY>

how to i put this into a PHP structure that makes sense? and when normalizing back, how do i keep the order?

do i need to use NormalizableInterface, DenormalizableInterface? And what's with the other attributes, do i need to manually (de)normalize them too then?

Upvotes: -1

Views: 113

Answers (1)

Michael Kay
Michael Kay

Reputation: 163587

Dealing with such structures in most APIs is tricky. I would start by transforming the data to a more regular structure using XSLT:

<xsl:for-each-group select="*" group-starting-with="EMAIL">
  <ENTRY email="{EMAIL}">
    <xsl:copy-of select="PUBLIC_KEY"/>
  </ENTRY>
</xsl:for-each-group>

which will turn it into:

<ENTRY email="A">
  <PUBLIC_KEY>A1</PUBLIC_KEY>
  <PUBLIC_KEY>A2</PUBLIC_KEY>
</ENTRY>
<ENTRY email="B">
  <PUBLIC_KEY>B1</PUBLIC_KEY>
  <PUBLIC_KEY>B2</PUBLIC_KEY>
</ENTRY>

As a general principle, if you have XML in a difficult-to-process form, it's best to clean it up as the first stage in processing to avoid complicating the logic of the real application.

Upvotes: 0

Related Questions