我需要使用xml元素屬性值進行重複檢查。xml元素中的重複檢查
樣品輸入請求:
<Parent>
<child id="1"> test 1</child>
<child id="1"> test 2</child>
<child id="2"> test 3</child>
</parent>
我想找到ID重複使用XSD或其他方式將fine.Anyone請幫我檢測到重複元件,它存在於請求XML子元素屬性屬性值。
我需要使用xml元素屬性值進行重複檢查。xml元素中的重複檢查
樣品輸入請求:
<Parent>
<child id="1"> test 1</child>
<child id="1"> test 2</child>
<child id="2"> test 3</child>
</parent>
我想找到ID重複使用XSD或其他方式將fine.Anyone請幫我檢測到重複元件,它存在於請求XML子元素屬性屬性值。
基本上,您需要使用xsd:unique來強制執行元素或屬性的唯一性。下面是從鏈路的摘錄:
指定屬性或元素值(或 屬性或元素值的組合)必須在規定的 範圍內是唯一的。該值必須是唯一的或零。
在情況下,如果你不知道,那麼你需要使用它,你可以強制執行這種唯一性,然後驗證您不要使用任何可用的XML解析器是XSD/DTD XML創建XSD/DTD。下面是一個Java示例以及XSD。
您的問題聲明:
請求XML使用XSD或其他方式將被罰款
按我所知,如果你想查詢你的XML文檔的有效性,然後您必須有XSD或DTD,沒有其他方式可以在沒有XSD或DTD(或更少了解RELAX NG)的情況下執行此操作。因此,您只需編寫一個定義XML文檔的預期結構的XSD或DTD,然後使用一些XML驗證程序來根據該XSD/DTD驗證您的XML文檔,它會告訴您XML文檔是否符合XSD/DTD與否。 底線是您需要編寫/指定一個XSD/DTD,它定義了XML文檔的預期結構。
你的XML: 「so.xml」
<Parent>
<child id="1"> test 1</child>
<child id="2"> test 2</child>
<child id="2"> test 3</child>
</Parent>
樣品XSD你需要(與XSD:獨特的您的要求): 「so.xsd」
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Parent">
<xs:complexType>
<xs:sequence>
<xs:element name="child" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:token" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<!-- This is the solution of your problem - "xs:unique" -->
<xs:unique name="unique-id">
<xs:selector xpath="child"/>
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
</xs:schema>
用於驗證的Java代碼示例:
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
public class XmlSchemaValidationHelper {
public static void main(String[] argv) {
XmlSchemaValidationHelper schemaValidationHelper = new XmlSchemaValidationHelper();
schemaValidationHelper.validateAgainstSchema(new File(argv[0]), new File(argv[1]));
}
public void validateAgainstSchema(File xmlFile, File xsdFile) {
try {
System.out.println("### Starting...");
// parse an XML document into a DOM tree
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder parser = builderFactory.newDocumentBuilder();
Document document = parser.parse(xmlFile);
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
Source schemaFile = new StreamSource(xsdFile);
Schema schema = factory.newSchema(schemaFile);
// create a Validator instance, which can be used to validate an
// instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
System.out.println("### Finished...");
} catch (FileNotFoundException ex) {
throw new OpenClinicaSystemException("File was not found", ex.getCause());
} catch (IOException ioe) {
throw new OpenClinicaSystemException("IO Exception", ioe.getCause());
} catch (SAXParseException spe) {
spe.printStackTrace();
throw new OpenClinicaSystemException("Line : " + spe.getLineNumber() + " - " + spe.getMessage(), spe.getCause());
} catch (SAXException e) {
throw new OpenClinicaSystemException(e.getMessage(), e.getCause());
} catch (ParserConfigurationException pce) {
throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
}
}
public class OpenClinicaSystemException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 1L;
private String errorCode;
private Object[] errorParams;
public OpenClinicaSystemException(String code, String message) {
this(message);
this.errorCode = code;
}
public OpenClinicaSystemException(String code, String message, Throwable cause) {
this(message, cause);
this.errorCode = code;
}
public OpenClinicaSystemException(String message, Throwable cause) {
super(message, cause);
}
public OpenClinicaSystemException(Throwable cause) {
super(cause);
}
public OpenClinicaSystemException(String message) {
super(message);
this.errorCode = message;
}
public OpenClinicaSystemException(String code, Object[] errorParams) {
this.errorCode = code;
this.errorParams = errorParams;
}
public OpenClinicaSystemException(String code, Object[] errorParams, String message) {
this(message);
this.errorCode = code;
this.errorParams = errorParams;
}
public String getErrorCode() {
return errorCode;
}
public Object[] getErrorParams() {
return errorParams;
}
public void setErrorParams(Object[] errorParams) {
this.errorParams = errorParams;
}
}
}
您可以運行此程序爲 - java XmlSchemaValidationHelper so.xml so.xsd
您是否檢查過:http://stackoverflow.com/questions/2386633/how-do-i-ensure-unique-element-values-in-an-xml-schema – HRgiger