简体   繁体   中英

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.

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)

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.

在此处输入图片说明

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM