【问题标题】:XSD for one element onlyXSD 仅适用于一个元素
【发布时间】:2014-04-16 07:39:10
【问题描述】:

如果没有给出表单 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>

问候

【问题讨论】:

  • 为了验证我使用了 maven 的 xml-maven-plugin

标签: java html validation jsf-2 xsd


【解决方案1】:

它找不到&lt;ui:composition&gt;,因为它没有在您的架构中声明。

如果您要验证 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

【讨论】:

  • 如果你想使用 XSD,另一个选择是重新声明组合元素而不导入任何东西
【解决方案2】:

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

【讨论】: