2015-04-01 65 views
2

这是在其中一个示例中提供的默认camel-context。我们是否需要为子标记提供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" 
     xmlns:camel="http://camel.apache.org/schema/spring" 
     xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> 

    <camel:camelContext xmlns="http://camel.apache.org/schema/spring" id="sampleCamelContext">  
    <camel:route> 
     <camel:from uri="file:src/data?noop=true"/> 
     <camel:choice> 
     <camel:when> 
      <camel:xpath>/person/city = 'London'</camel:xpath> 
      <camel:log message="UK message"/> 
      <camel:to uri="file:target/messages/uk"/> 
     </camel:when> 
     <camel:otherwise> 
      <camel:log message="Other message"/> 
      <camel:to uri="file:target/messages/others"/> 
     </camel:otherwise> 
     </camel:choice> 
    </camel:route> 
    </camel:camelContext> 
</beans> 

现在既然提供了camel:camelContext默认NS,我们需要在子标签也可以指定前缀?

这不工作? (如https://stackoverflow.com/a/25789100/1873328提到)

<camel:camelContext xmlns="http://camel.apache.org/schema/spring" id="sampleCamelContext">  
    <route> 
     <from uri="file:src/data?noop=true"/> 
     <choice> 
     <when> 
      <xpath>/person/city = 'London'</camel:xpath> 
      <log message="UK message"/> 
      <to uri="file:target/messages/uk"/> 
     </when>   
     </choice> 
    </route> 
</camel:camelContext> 

也许,我不正确理解XML命名空间。值得赞赏的是,有关名称空间如何作为奖励的任何详细解释。

回答

1

以下两个事实给你答案:

  • 子节点使用从最近的父节点xmlns声明。
  • 前缀文字(例如camel)并不重要 - 唯一重要的是它引用的名称空间。如果不同的前缀引用相同的名称空间,则它们可以交换使用,而不具有前缀也是前缀(空的)。

因为你xmlns宣言camel:camelContext是指在相同的命名空间xmlns:camel声明,它不一样,如果你省略camel:这些子节点与否,他们将反对http://camel.apache.org/schema/spring命名空间中列出两种情况下无所谓。

+0

因此,如果我理解正确,因为''有一个xmlns定义,所以没有任何前缀的子元素将继承这个名称空间。所以,我可以跳过''本身的骆驼前缀,因为它已经定义了xmlns。 – 2015-04-01 16:39:29

+0

是的,你可以。或者你可以放弃它,因为它可以提高可读性。 – GSerg 2015-04-01 16:44:36

相关问题