无法针对xsd架构验证xml doc(无法找到元素'replyMessage'的声明)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法针对xsd架构验证xml doc(无法找到元素'replyMessage'的声明)相关的知识,希望对你有一定的参考价值。

我使用以下代码来验证针对XSD架构的XML文件

package com.forat.xsd;

import java.io.IOException;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XSDValidate {

 public void validate(String xmlFile, String xsd_url) {
  try {
   SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
   Schema schema = factory.newSchema(new URL(xsd_url));
   Validator validator = schema.newValidator();
   ValidationHandler handler = new ValidationHandler();
   validator.setErrorHandler(handler);
   validator.validate(getSource(xmlFile));

   if (handler.errorsFound == true) {
    System.err.println("Validation Error : "+ handler.exception.getMessage());
   }else {
    System.out.println("DONE");
   }
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

 private Source getSource(String resource) {
  return new StreamSource(XSDValidate.class.getClassLoader().getResourceAsStream(resource));
 }

 private class ValidationHandler implements ErrorHandler {
  private boolean errorsFound = false;
  private SAXParseException exception;

  public void error(SAXParseException exception) throws SAXException {
   this.errorsFound = true;
   this.exception = exception;
  }

  public void fatalError(SAXParseException exception) throws SAXException {
   this.errorsFound = true;
   this.exception = exception;
  }

  public void warning(SAXParseException exception) throws SAXException {
  }
 }

 /*
  * Test
  */
 public static void main(String[] args) {
  new XSDValidate().validate("com/forat/xsd/reply.xml", 
    "https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.53.xsd"); // return error
 }

}

如图所示,它是一个标准代码,尝试验证以下XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<replyMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <merchantReferenceCode>XXXXXXXXXXXXX</merchantReferenceCode>
 <requestID>XXXXXXXXXXXXX</requestID>
 <decision>XXXXXXXXXXXXX</decision>
 <reasonCode>XXXXXXXXXXXXX</reasonCode>
 <requestToken>XXXXXXXXXXXXX
 </requestToken>
 <purchaseTotals>
  <currency>XXXXXXXXXXXXX</currency>
 </purchaseTotals>
 <ccAuthReply>
  <reasonCode>XXXXXXXXXXXXX</reasonCode>
  <amount>XXXXXXXXXXXXX</amount>
  <authorizationCode>XXXXXXXXXXXXX</authorizationCode>

  <avsCode>XXXXXXXXXXXXX</avsCode>
  <avsCodeRaw>XXXXXXXXXXXXX</avsCodeRaw>
  <authorizedDateTime>XXXXXXXXXXXXX</authorizedDateTime>
  <processorResponse>0XXXXXXXXXXXXX</processorResponse>
  <authRecord>XXXXXXXXXXXXX
  </authRecord>
 </ccAuthReply>
</replyMessage>

针对以下XSD:

https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor/CyberSourceTransaction_1.53.xsd

错误是:

验证错误:cvc-elt.1:找不到元素'replyMessage'的声明。

答案

您的XML无效,因为它不在架构所需的命名空间中:

targetNamespace="urn:schemas-cybersource-com:transaction-data-1.53"

您需要将根元素的开始标记更改为:

<replyMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns="urn:schemas-cybersource-com:transaction-data-1.53">

以上是关于无法针对xsd架构验证xml doc(无法找到元素'replyMessage'的声明)的主要内容,如果未能解决你的问题,请参考以下文章

在 C# 中针对引用的 XSD 验证 XML

PHP 5.3.3 无法针对嵌套的 XSD 验证 XML

验证 XSD 架构?

针对同一 XML 模式 (XSD) 加快一批 XML 文件的 XML 模式验证

在 C# 中针对 XSD 模式验证 json 数据

具有相同元素名称但属性值不同的XML的XSD架构[关闭]