Pregunta

I log my bicycle rides manually into my PC, in XML format - and parse it with my own PHP script.

I know that I can force position of elements in code - with (for example):

<!ELEMENT Ride (Start, Through*, Finish) >

Element Ride has following attributes

<!ATTLIST Ride  Number CDATA #REQUIRED
        Purpose CDATA #IMPLIED
        Comment CDATA #IMPLIED
        Date CDATA #IMPLIED >

And my question is, if I can say that all those attributes can be in only this sequence - even if three of them are only implied (not neccessary).

Purpose of this is, that if I write wrong sequence of attributes, PHP script is not able to parse it - because it is written only for this sequence. And then ride with such wrong sequence of attributes is not included into export and parsing.

Current regex for parsing of intro data of ride is:

/\<Ride Number="([0-9]*)"( Purpose="([^\"]*)")?( Comment="([^\"]*)")?( Date="([^\"]*)")?\>(.*)\<\/Ride\>/sU

I thought also about change of that regex but it could be too long ... and thus not well readable.

¿Fue útil?

Solución

No, neither XML DTDs nor any other XML schema language I'm familiar with provide a way to constrain the order of attributes. As stwissel has already pointed out, the XML specification says that the order of attributes is not significant.

Your options include:

  • writing a filter to accept XML with attributes in any order and emit the same XML with the attributes in the order you wish them to appear;
  • improving (or at least: changing) your existing PHP code to accept attribute in arbitrary sequence;
  • rewriting your PHP code to use an XML-aware toolkit that makes it easier to deal with the non-determinism of attribute order;
  • redesigning the XML vocabulary to make the attributes in question appear as child elements, so that you can force them to appear in a prescribed sequence.

I would not go so far as to say that XML and regular expressions don't mix. But when you apply regular expressions to XML, you do need to deal with the basic facts of XML syntax.

Otros consejos

Regex and XML don't mix. The XML specification states that the sequence of attributes is not significant:

 the order of attribute specifications in a start-tag or empty-element tag is not significant

Check the XML Specification Section 3.1

Instead of a Regex you need to use a PHP XML expression. Simple XML might be good enough.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top