0

I have the following example xml:

<ip_addresses>
   <ip_address>
      <type>local</type>
      <ip>192.168.1.1</ip>
   </ip_address>

   <ip_address>
      <type>public</type>
      <ip>82.125.1.1</ip>
   </ip_address>
</ip_addresses>

How can I use different regex in my xml schema for ip checks in dependency of the value in <type> tag?

I also tried something like <ip type="local">192.168.1.1</ip> but I couldn't get the xml schema working correct.

thx for helping Marten

Marten Bauer
  • 3,099
  • 5
  • 22
  • 18
  • Why regex and not xpath? – Alex Filipovici Jul 10 '13 at 08:34
  • I mean regex for ip validation. I couldn't validate an ip by xpath or? Something like this value="(192)\.(168)\.(112)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) – Marten Bauer Jul 10 '13 at 08:35
  • Ok, now I see what you mean. – Alex Filipovici Jul 10 '13 at 08:37
  • possible duplicate of [In XSD, can I set mandatory child elements based on parent element's content?](http://stackoverflow.com/questions/8868366/in-xsd-can-i-set-mandatory-child-elements-based-on-parent-elements-content). Also, check this [answer](http://stackoverflow.com/a/787629/674700). – Alex Filipovici Jul 10 '13 at 08:58

1 Answers1

0

In XSD 1.0 you can't make the type of one element conditional on the value of another.

In XSD 1.1 you can do this using assertions. For example (at the level of ip_address) you could write

<xs:assert test="(@type='local' and @ip='192.168.1.1') or @type='public'"/>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • But @ip='XYZ' couldn't be a regex like in my comment above or? As I understand your answer I can only assert against one ip per xs:assert or? – Marten Bauer Jul 10 '13 at 10:16
  • No, you can use any XPath expression, for example you can use matches() with a regex. I'm not sure why you suggested a regex, there doesn't seem anything in the OP requirement that needs it, though the statement of the problem is very vague. – Michael Kay Jul 10 '13 at 20:24