其他分享
首页 > 其他分享> > 有关可以添加到XML文档中的元素的XML名称空间信息?

有关可以添加到XML文档中的元素的XML名称空间信息?

作者:互联网

我对XML的命名空间有疑问.考虑一下我在spring应用程序中拥有的xml名称空间:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
    xmlns:context="http://www.springframework.org/schema/context">

该服务器仅用作命名空间(避免命名冲突的方式)的目的,还是可以在xml文档中添加哪些元素?

我正在添加元素(休眠/弹簧配置),它抱怨我们需要添加一些名称空间?如果命名空间仅用作避免命名冲突的一种方式(XSD告诉所有元素可以包含在xml文档中).我对此有疑问,为什么要添加一个Spring期望的名称空间(就任何xml解析器而言)将能够获取该元素. XSD的工作不是告诉XML文档中可以包含哪些所有元素吗?

我对此表示怀疑,任何澄清都会有所帮助.

我做了谷歌得到我的答案,但不满意,因为无法消除疑问.

解决方法:

“Does this server a purpose only as namespace (the way to avoid the naming conflict) OR what elements can be added into the xml document?”

后者.

Namesapces用于标识架构定义中的元素和属性.

“I was adding element (hibernate/spring configuration) and it complained that we need to add some namespace?”

使用Spring进行持久化,通常需要使用spring-orm jar,spring-jdbc,spring-tx,可能还需要其他一些.通常,所有spring-xxx jar都带有其架构定义.如上所述,如果要在该特定模式中使用元素,则需要在上下文文件中添加名称空间.

您当前拥有的名称空间只是bean和上下文名称空间.如果查看xsds,将看到这些名称空间允许的所有顶级元素.例如,bean名称空间仅允许< alias&gt ;、< bean&gt ;、< beans&gt ;、< description>和< import>.而且上下文名称空间仅支持

<context:annotation-config>
<context:component-scan>
<context:load-time-weaver>
<context:mbean-export>
<context:mbean-server>
<context:property-override>
<context:property-placeholder>
<context:spring-configured>

由于bean是文档的默认名称空间

<beans xmlns="http://www.springframework.org/schema/beans"

您不需要像< context:component-scan>那样为元素加上前缀(例如< beans:bean>).

至于您当前的问题,“它抱怨我们需要添加一些名称空间”……您实际上没有提供足够的信息来帮助您解决问题,但是通常在进行持久性处理时,您将需要tx名称空间来处理事务,并且如果要使用嵌入式数据库,则可能需要jdbc命名空间以及< jdbc:embedded-database>元件.

这只是一般的猜测工作.

” I have this doubts how come adding a namespace which spring expects (for the matter any xml parser) would be able to get the element. Isn’t it the job of XSD which tells what all elements can be contained in an XML document?”

对于您的误解有些不清楚,但是就像基于架构的任何其他xml一样,它需要进行验证.模式就像类定义.类定义是该类实例所允许的约定.

Spring使用上下文来创建您定义的所有bean.如果定义不正确,Spring可能不知道如何处理您的xml.这就是我们拥有模式的原因-要遵循其准则,更高级别的应用程序需要您遵循才能工作.

Spring要做的就是获取您的xml文件,并使用相应的NamespaceHandler,它能够找到您需要的解析器.解析器的工作是创建bean定义,该定义允许Spring容器实例化bean

流程示例:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="http://www.springframework.org/schema/oxm 
                http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd">

    <oxm:jaxb2-marshaller>
        <oxm:class-to-be-bound name="com.underdogdevs.spring.oxm.Application" />
    </oxm:jaxb2-marshaller>

</beans>

>对jaxb2-marshaller使用oxm名称空间,该名称空间在jar包之一的spring-oxm-4.0.xsd中定义.
>请注意schemaLocation http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd. Spring将使用它来确定处理程序.
>查看每个spring-xxx.jar中包含的META-INF,您将找到spring.schemas和spring.handlers文件.在那里你会发现

http\://www.springframework.org/schema/oxm/
      spring-oxm-4.0.xsd=org/springframework/oxm/config/spring-oxm-4.0.xsd

http\://www.springframework.org/schema
     /oxm=org.springframework.oxm.config.OxmNamespaceHandler

这告诉spring验证哪个模式,使用的名称空间处理程序分别是OxmNamespaceHandler.
>如果我们查看OxmNamespaceHandler类,您会发现

registerBeanDefinitionParser("jaxb2-marshaller",
                             new Jaxb2MarshallerBeanDefinitionParser());

现在发生的事情是名称空间处理程序将我们定向到正确的解析器.
>现在看看Jaxb2MarshallerBeanDefinitionParser

@Override
protected String getBeanClassName(Element element) {
    return "org.springframework.oxm.jaxb.Jaxb2Marshaller";
}

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder beanDefinitionBuilder) {
    String contextPath = element.getAttribute("context-path");
    if (!StringUtils.hasText(contextPath)) {
        // Backwards compatibility with 3.x version of the xsd
        contextPath = element.getAttribute("contextPath");
    }
    if (StringUtils.hasText(contextPath)) {
        beanDefinitionBuilder.addPropertyValue("contextPath", contextPath);
    }

    List<Element> classes = DomUtils.getChildElementsByTagName(element, "class-to-be-bound");
    if (!classes.isEmpty()) {
        ManagedList<String> classesToBeBound = new ManagedList<String>(classes.size());
        for (Element classToBeBound : classes) {
            String className = classToBeBound.getAttribute("name");
            classesToBeBound.add(className);
        }
        beanDefinitionBuilder.addPropertyValue("classesToBeBound", classesToBeBound);
    }
}

需要注意的有趣一点是getBeanClassName方法,该方法返回“ org.springframework.oxm.jaxb.Jaxb2Marshaller”.这就是Spring知道要创建哪个bean的方式.另外,如果您查看上面的上下文文件,您将看到元素< oxm:class-to-be-bound>.这是< oxm:jax2b-marshaller>模式定义的一部分.元件.您也可以在doParse()方法中看到它-getChildElementsByTagName(element,“要绑定的类”);.

所有这些都是为了确保我们最终获得一个很好的Jaxb2marshaller实例.它与Spring的所有内容几乎相同

因此,您可以看到Spring背后有很多事情要做,并希望您知道为什么需要名称空间.

标签:xsd,xml,spring,xml-parsing
来源: https://codeday.me/bug/20191121/2050315.html