danielperaza
danielperaza

Reputation: 412

Make an xml element's content unique in a XSD Instance Document

I'm currently trying to develop a XSD Schema for invoicing information which can be seen at http://intranet.servofarma.com/xml/schema/facturas.xsd.

My question is how can I make the contents of nroFactura element unique across a XSD instance document?. I tried to use <unique> but it is not clear to me how to use this constraint.

Upvotes: 0

Views: 136

Answers (2)

Petru Gardea
Petru Gardea

Reputation: 21638

I don't think you can get a more concise answer than @Michael's (+1); still, I think you might also benefit from these additional clarifications.

Given your XSD, the Y in Michael's 'notation' can only be facturas, since it is the only element in your XSD.

enter image description here

The modification:

<element name="facturas" type="tns:facturasType">
    <unique name="pk1">
        <selector xpath="tns:factura/tns:nroFactura"/>
        <field xpath="."/> 
    </unique>
</element>

The result then looks like this:

enter image description here

One more thing I would mention is the use of .// vs. being specific. The former is a great way to get things started; it may prove also hard to deal with in large XSDs where tags may be "reused" in different context. I would say it is a better practice to be as specific as possible than trying to match everything... unless, of course, that is the requirement.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163262

The "unique" constraint in XSD allows you to say "every X within a given Y must have a unique value for Z". The constraint goes on the definition of Y. The xs:selector defines an XPath expression to select X from Y (for example, .//nroFactura), and the xs:field defines an XPath expression to select Z from X (for example, "." selects the string value of the element itself)

Upvotes: 1

Related Questions