I am new to XML and I have been stuck for almost a week today.
I have one XML file which contains the schema and xml elements, in my understanding. I am trying to get values that are defined in the schema and use them, I also would like to know how to join xml elements, as I would in mysql or sql by saying join table a with table b where a.id=b.a_id
but in this case with xml elements.
Here is the structure I have:
and the Product object as follows:
`<xs:element name="ProductImage">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductBaseCode" default="">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="15" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ImageName">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="IsDefaultImage" type="xs:boolean" />
<xs:element name="ImageWidth" type="xs:int" />
<xs:element name="ImageHeight" type="xs:int" />
<xs:element name="ImageUrl">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<Product>
<Name>Pad Printing</Name>
<Description>Pad Printing</Description>
<Price>0.8</Price>
<Flags>1</Flags>
<BaseItemCode>PA</BaseItemCode>
<Id>1b9d8241-61c8-47cd-baf5-d9258cbc4734</Id>
<ColourName/>
</Product>
`
I need to get the ImageURL
in ProductImage for the relevant Product using the BaseItemCode
which is ProductBaseCode
in the ProductImage
If you wanna read data within an XML you can use simplexml_load_string.
You can do like this:
$xml = "<xs:element name="ProductImage">
<xs:complexType>
<xs:sequence>
<xs:element name="ProductBaseCode" default="">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="15" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="ImageName">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="IsDefaultImage" type="xs:boolean" />
<xs:element name="ImageWidth" type="xs:int" />
<xs:element name="ImageHeight" type="xs:int" />
<xs:element name="ImageUrl">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="255" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<Product>
<Name>Pad Printing</Name>
<Description>Pad Printing</Description>
<Price>0.8</Price>
<Flags>1</Flags>
<BaseItemCode>PA</BaseItemCode>
<Id>1b9d8241-61c8-47cd-baf5-d9258cbc4734</Id>
<ColourName/>
</Product>"
$xmlData = simplexml_load_string($xml);
$imageurl = $xmldata['xs:element']['xs:element'][5]['@attributes']['name'];
So basically you make the XML into an object and then, go to the place in the object you want some data from.
If you are not sure how you object structure is, you can always just print the object.
It's unusual for the schema to be embedded within the instance document. I don't think you need it and I think you are best off ignoring it.
What confuses me is that you've shown the schema for a ProductImage element, but there are no actual ProductImage elements shown. Are they in the same document as the Product elements, or somewhere else?
In the PHP world the simplest way of doing the kind of join query you describe is probably to use XSLT. There's a native XSLT 1.0 processor built-in, or you could install Saxon/C which has a PHP interface giving you XSLT 3.0. It also gives you XQuery, which you might find more comfortable working with if you are someone who thinks of things in SQL terms. I can't give you any actual XQuery code to reflect your query because I can't see where the data is coming from.