-1

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?

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I need to experiment with this --- but i also need to find a way to do it in the opposite direction (PHP to XML)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.