标签: xml-namespaces

为什么我必须将空名称空间传递给XPathNavigator.GetAttribute?

给定以下XML标记:

<root xmlns="Demo">
    <child name="foo"/>
</root>
Run Code Online (Sandbox Code Playgroud)

并且XPathNavigator位于<child>元素上,

string withNs = navigator.GetAttribute("name", navigator.NamespaceURI);
string withoutNs = navigator.GetAttribute("name", "");
Run Code Online (Sandbox Code Playgroud)

产生奇怪的结果:withNs是空的,withoutNs包含foo.

这是为什么?我原以为它会反过来,因为name属性必须Demo像child元素一样在命名空间中.

MSDN文档没有提到传递的任何神奇含义namespaceURI="",所以我假设你必须传递属性的真实命名空间URI.

.net xml xml-namespaces xpathnavigator

4
推荐指数
1
解决办法
3392
查看次数

GDataXML使用xmlns命名空间写入文件

我正在使用GDataXML,我遇到了编写XML文件的一些问题.我按照Raywenderlich的教程,但我必须添加一些名称空间.

例如:
[FooElement addChild:bar1Element];
[FooElement addChild:bar2Element];
[FooElement addChild:bar3Element];
[FooElement addChild:bar4Element];

我想做类似的事情:
[FooElement addNamespace:@"xmlns ="https:// foo/bar /"

将此结果 存入文件:
<Foo xmlns ="https:// foo/bar /" >
<bar1> xxxx </ bar1>
<bar2> xxxx </ bar2>
<bar3> xxxx </ bar3>
<bar4> xxxx </ bar4>
</ Foo>

感谢帮助 !

namespaces xml-namespaces gdataxml

4
推荐指数
1
解决办法
1004
查看次数

Spring的上下文命名空间如何工作?

例如,如果我在application-context.xml中声明:

<context:annotation-config/>
Run Code Online (Sandbox Code Playgroud)

我从官方文件中读到:

在隐式注册后处理器包括AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor会,PersistenceAnnotationBeanPostProcessor,以及上述RequiredAnnotationBeanPostProcessor.

但我想知道Spring是如何工作的,我认为这个1-liner被转换为文档中提到的后处理器的几个bean定义.

但是,我的问题是,哪个Spring组件/类实现了"从1-liner到多个bean定义的转换"功能?

java spring xml-namespaces post-processing applicationcontext

4
推荐指数
1
解决办法
2805
查看次数

XML没有获取<camelContext>标记

这可能是一个简单的错误配置问题,但我的xml文档拿起我的标签时遇到问题.我收到了错误

"在这一行找到了多个注释: - cvc-complex-type.2.4.c:匹配的通配符是严格的,但是没有找到元素'camelContext'的声明. - 无法找到模式元素'camelContext'的Spring NamespaceHandler命名空间'http://activemq.apache.org/camel/"

但是当我添加命名空间时,我收到以下错误:

在此行找到多个注释: - cvc-complex-type.2.4.c:匹配的通配符是strict,但是找不到元素'camelContext'的声明. - schema_reference.4:无法读取模式文档"http://activemq.apache.org/camel/schema/spring/camel-spring.xsd",因为1)找不到文档; 2)文件无法阅读; 3)文档的根元素不是. - 无法找到模式名称空间"http://activemq.apache.org/camel/schema/spring"的元素"camelContext"的Spring NamespaceHandler

任何人都可以帮我弄清楚为什么我会遇到这两个问题?我不是xml或骆驼的专家,所以任何帮助将不胜感激.下面是我的简单xml文档:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://activemq.apache.org/camel/schema/spring     http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
      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">



   <bean id="myrouter"      class="org.apache.camel.example.reportincident.ReportIncidentRoutes"/>



<!-- Camel Configuration -->

 <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
    <routeBuilderRef ref="myrouter"/>


 </camelContext>

</beans>
Run Code Online (Sandbox Code Playgroud)

xml spring apache-camel xml-namespaces

4
推荐指数
1
解决办法
7752
查看次数

XSLT命名空间

我有一个关于XSLT命名空间的基本问题.
XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="myNamespace" exclude-result-prefixes="x">
    <xsl:template match="/">
        <newNode>
            <xsl:value-of select="x:Node1/x:Node2" />
        </newNode>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当我将XSLT应用于:

<Node1 xmlns="myNamespace">
    <Node2>ValueIWant</Node2>
</Node1>
Run Code Online (Sandbox Code Playgroud)

但是当我将它应用于时,它找不到"ValueIWant":

<ns0:Node1 xmlns:ns0="myNamespace">
    <Node2>ValueIWant</Node2>
</ns0:Node1>
Run Code Online (Sandbox Code Playgroud)

我觉得我只是缺少对XSLT命名空间的一些基本了解.任何帮助是极大的赞赏.

xml xslt xml-namespaces

4
推荐指数
1
解决办法
225
查看次数

XPath如何以名称空间不知情的方式识别谓词中的属性

我有以下XML文件:

<foo:a xmlns:foo=\"http://www.foo.com\">
    <foo:b foo:bar=\"zar\">
    </foo:b>
</foo:a>
Run Code Online (Sandbox Code Playgroud)

要获取具有值为"zar" 的属性栏的b节点(全部在正确的命名空间中),我可以使用XPath表达式:

/foo:a/foo:b[@foo:bar=\"zar\"]
Run Code Online (Sandbox Code Playgroud)

("foo"正确绑定到" http://www.foo.com " - 请参阅末尾的代码)

但是,当我想以命名空间不知道的方式做同样的事情时,虽然我可以通过依赖local-name()函数从元素中删除命名空间,但是我无法从属性中删除它们.

这是我能想到的最好的:

/*[local-name()='a']/*[local-name()='b' and @foo:bar=\"zar\"]
Run Code Online (Sandbox Code Playgroud)

(foo令人遗憾的是,用于限定bar属性的地方).

如何在完全删除命名空间的情况下编写上述表达式?

下面的代码已经在CLASSPATH上使用和不使用Saxon-HE-9.4.jar进行了测试并产生了正确的结果,但我无法从第二个XPath表达式中获取'foo'命名空间前缀!

import java.io.*;
import java.util.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.namespace.NamespaceContext;

public class FooMain {

    public static void main(String args[]) throws Exception {

        String xmlSample = "<foo:a xmlns:foo=\"http://www.foo.com\"><foo:b foo:bar=\"zar\"></foo:b></foo:a>";
        XPath xpath = namespaceAwareXpath("foo", "http://www.foo.com");
        {
            System.out.printf("[NS-aware           ] Number of 'b' nodes with foo:bar attribute …
Run Code Online (Sandbox Code Playgroud)

xml xpath xml-namespaces

4
推荐指数
1
解决办法
5123
查看次数

XmlNode.InnerXml属性 - 省略xmlns属性

我有一些代码来替换XML文档的根节点名称,同时保留其名称空间.

XmlDocument doc = new XmlDocument();
Stream inStream = inmsg.BodyPart.GetOriginalDataStream();
doc.Load(inStream);

XmlNode root = doc.DocumentElement;
XmlNode replacement = doc.SelectSingleNode("/*/*[1]");

XmlNode newRoot = doc.CreateElement(replacement.Name);
XmlAttribute xmlns = (XmlAttribute)root.Attributes["xmlns"].Clone();
newRoot.Attributes.Append(xmlns);
newRoot.InnerXml = root.InnerXml; //the problem is here!

doc.ReplaceChild(newRoot, root);
Run Code Online (Sandbox Code Playgroud)

使用如下所示的文档:

<OLD_ROOT xmlns="http://my.xml.namespace">
    <NEW_ROOT>
Run Code Online (Sandbox Code Playgroud)

它导致:

<NEW_ROOT xmlns="http://my.xml.namespace">
     <NEW_ROOT xmlns="http://my.xml.namespace">
Run Code Online (Sandbox Code Playgroud)

第二个xmlns是因为该InnerXml属性显然将其设置在其内容的第一个节点上!我可以做些什么来规避这一点,而不必在事后删除它?

之后无法删除它:

尝试使用以下代码

XmlNode first_node = doc.SelectSingleNode("/*/*[1]");
XmlAttribute excess_xmlns = first_node.Attributes["xmlns"];
first_node.Attributes.Remove(excess_xmlns);
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为xmlns显然不存在该节点上的属性!

.net c# xml xmldocument xml-namespaces

4
推荐指数
1
解决办法
2250
查看次数

XML序列化命名空间

我的代码产生的命名空间有问题.我想要的是下面的XML:

<?xml version="1.0" encoding="utf-8"?>
<ClassToSerialize Type="Customer" Name="Some Name" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.123.org/namespace C:\Schema\ClassToSerialize.xsd" 
xmlns:Test="http://www.Test.org/" xmlns="http://www.nrf-arts.org/namespace">
  <Address>
    <Line1>Addr1</Line1>
    <Line2>Addr2</Line2>
  </Address>
</ClassToSerialize>
Run Code Online (Sandbox Code Playgroud)

我得到的是这个XML:

<?xml version="1.0" encoding="utf-8"?>
<ClassToSerialize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="http://www.123.org/namespace C:\Schema\ClassToSerialize.xsd"
 xmlns:Test="http://www.Test.org/" xmlns:xmlns="http://www.nrf-arts.org/namespace" Type="Customer" Name="Some Name">
  <Address>
    <Line1>Addr1</Line1>
    <Line2>Addr2</Line2>
  </Address>
</ClassToSerialize>
Run Code Online (Sandbox Code Playgroud)

主要区别是:

1. xmlns:schemaLocation= needs to be xsi:schemaLocation=
2. xmlns:xmlns= needs to be xmlns=
3. Attributes Order, I would prefer the Attributes to be presented before the namespace attributes (This is not a big Issue, just nice to have)
Run Code Online (Sandbox Code Playgroud)

目前我正在做的事情是以上我想要的值替换序列化的字符串值在1和2,一个可行的技巧,但我怀疑有修改的命名空间代码在这一点上做到这一点的方法吗?

这是我正在使用的代码,如何更改GetNameSpace()以在第1点和第2点执行我需要的操作,还可以重新排序属性吗?:

public partial class Form1 …
Run Code Online (Sandbox Code Playgroud)

c# xml xml-namespaces

4
推荐指数
1
解决办法
766
查看次数

在Node.js中使用xmlbuilder在xml中添加namspace

我正在使用xmlbulilder包在nodejs中生成xml,现在我的要求是向xml添加名称空间。例如

<nsA:root xmlns:nsA="namespaceA" xmlns:nsB="namespaceB">
    <nsB:nodeA attrC="valC">nodeText</nsB:nodeA>
</nsA:root>
Run Code Online (Sandbox Code Playgroud)

我们该怎么做?感谢帮助!

xml xml-namespaces xml-builder node.js

4
推荐指数
1
解决办法
1231
查看次数

lxml xsi:schemaLocation名称空间URI验证问题

我正在尝试lxml.etree重现此处找到的CDA快速入门指南中的CDA示例.

特别是,我遇到了尝试重新创建此元素的命名空间问题.

<ClinicalDocument xmlns="urn:hl7-org:v3" xmlns:mif="urn:hl7-org:v3/mif" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="urn:hl7-org:v3 CDA.xsd">
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码如下

root = etree.Element('ClinicalDocument',
                    nsmap={None: 'urn:hl7-org:v3',
                           'mif': 'urn:hl7-org:v3/mif',
                           'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
                           '{http://www.w3.org/2001/XMLSchema-instance}schemaLocation': 'urn:hl7-org:v3 CDA.xsd'})
Run Code Online (Sandbox Code Playgroud)

问题在于schemaLocation进入nsmap.lxml似乎试图验证该值并给出错误

ValueError: Invalid namespace URI u'urn:hl7-org:v3 CDA.xsd'
Run Code Online (Sandbox Code Playgroud)

我是否schemaLocation错误地指定了值?有没有办法强制lxml接受任何字符串值?或者示例中的值是否只是一个占位符,我应该用其他东西替换?

python xml lxml xml-namespaces cda

4
推荐指数
1
解决办法
1657
查看次数