2017-01-02 28 views
0

xml要素の属性値で重複チェックを行う必要があります。xml要素内の重複チェック

サンプル入力要求:

<Parent> 
    <child id="1"> test 1</child> 
    <child id="1"> test 2</child> 
    <child id="2"> test 3</child> 
</parent> 

私はXSDを使用してリクエストXML内の子要素の属性に存在しているか、他の方法は、重複する要素を検出するために、私を助けてくださいfine.Anyoneされるだろう、重複IDを見つけたいです属性値。

+0

が持っているあなたがチェック:http://stackoverflow.com/questions/2386633/how-do-i-ensure-unique-element-values-in-an-xml-schema – HRgiger

答えて

1

基本的には、要素または属性の一意性を強制するためにxsd:uniqueを使用する必要があります。以下のリンクからの抜粋である:

は属性または要素値(または 属性または要素値の組み合わせ)が指定 範囲内で一意でなければならないことを指定します。値は一意またはnilでなければなりません。場合

あなたが気づいていないならば、あなたは、この一意性を強制して、利用できるXMLパーサのいずれかを使用して、そのXSD/DTDと照らし合わせてXMLを検証することができた使用してXSD/DTDを作成する必要があります。以下は、Javaの例とXSDです。問題の

あなたの声明:あなたは、その有効性のためにあなたのXMLドキュメントをチェックしたい場合は、XSDまたはいくつかの他の方法を使用して

要求XMLは、私の知る限り1として

いいと思い、その後XSDまたはDTDのいずれかでなければなりません.XSDまたはDTD(あるいはそれほど知られていないRELAX NG)を持たないと他の方法で行うことはできません。そのため、XML文書の予想される構造を定義するXSDまたはDTDを作成し、XMLバリデーターを使用してそのXML文書をそのXSD/DTDに対して検証し、XML文書が従うかどうかを伝えるだけですXSD/DTDかどうか。 要点は、XML文書の予想される構造を定義するXSD/DTDを記述/指定する必要があるということです。

あなたの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