我需要使用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