XSD仅适用于一个元素

时间:2014-03-12 09:24:00

标签: java html validation jsf-2 xsd

如果没有给出form-id,我希望有一个例外。

但这会抛出异常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>

此致

2 个答案:

答案 0 :(得分:0)

它无法找到<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

答案 1 :(得分:0)

使用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);
            }
        }
    }
}