仅适用于一个元素的XSD

问题描述:

如果没有指定form-id,我喜欢有异常。仅适用于一个元素的XSD

但是这会引发异常Cannot find the declaration of element 'ui:composition'

<?xml version="1.0" encoding="UTF-8"?> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://java.sun.com/jsf/html" 
    elementFormDefault="qualified"> 
    <complexType name="form"> 
     <attribute name="id" use="required"/> 
    </complexType> 
</schema> 

这是我的XHTML我验证对:

<ui:composition template="/template/overall.xhtml" 
       xmlns="http://www.w3.org/1999/xhtml" 
       xmlns:ui="http://java.sun.com/jsf/facelets" 
       xmlns:f="http://java.sun.com/jsf/core" 
       xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"     
       xmlns:h="http://java.sun.com/jsf/html"> 
    ... <h:form id="Make_sure_i_exists"> ... 
</ui:composition> 

问候

+0

验证我使用maven的'xml-maven-plugin'。 –

要使用XSD是不可能的。我是通过单元测试做的(猜测它是一个很好的匹配用例)。

public class IdFullQualifiedTest extends DefaultHandler2 { 
    public final static List<String> NEED_ID = Arrays.asList(new String[] { 
      "h:form", "h:inputText", "h:commandButton", "a4j:include", 
      "h:dataTable" }); 

    public void testStructure() throws ParserConfigurationException, 
      SAXException, FactoryConfigurationError, IOException { 
     Iterator<File> iterateFiles = FileUtils.iterateFiles(new File("src"), 
       new String[] { "xhtml" }, true); 
     SAXParserFactory f = SAXParserFactory.newInstance(); 
     SAXParser p = f.newSAXParser(); 
     while (iterateFiles.hasNext()) { 
      p.parse(iterateFiles.next(), this); 
     } 
    } 

    private Locator loc; 

    @Override 
    public void setDocumentLocator(Locator loc) { 
     this.loc = loc; 
    } 

    @Override 
    public InputSource resolveEntity(String name, String publicId, 
      String baseURI, String systemId) throws SAXException, IOException { 
     return new InputSource(new StringReader("")); 
    } 

    @Override 
    public void startElement(String arg0, String d, String qname, 
      Attributes attrs) throws SAXException { 
     int idx = NEED_ID.indexOf(qname); 
     if (idx >= 0) { 
      if (attrs.getIndex("id") < 0) { 
       throw new SAXParseException(
         NEED_ID.get(idx) + " has no id (" + loc.getSystemId() 
           + ":" + loc.getLineNumber() + ") .", loc); 
      } 
     } 
    } 
} 

它无法找到<ui:composition>因为它不是在你的模式中声明。

如果您要验证从JSF架构元素,你必须将其导入:

<schema xmlns="http://www.w3.org/2001/XMLSchema" 
     targetNamespace="http://java.sun.com/jsf/html" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     elementFormDefault="qualified"> 

    <xs:import namespace="http://java.sun.com/jsf/facelets" schemaLocation="http:// ... /jsf-facelets_2_0.xsd" /> 

    <complexType name="form"> 
     <attribute name="id" use="required"/> 
    </complexType> 
</schema> 

你会发现在你的执行文件或JAR文件的jsf-facelets_2_0.xsd

+0

您的回答有太多要求。 –

+0

如果你想使用XSD,你拥有的另一个选择是重新声明组合元素,而不是导入任何东西 – helderdarocha