我制作了一个解析 XML 文件的 PHP 脚本,当我尝试解析它时,出现错误:
2: DOMDocument::load(): 表示信息的命名空间前缀 edf 未在 /users/zzz/testing/meta.xml 中定义,行:2
我一直在寻找解决方案,但找不到任何解决方案,所以我在这里发布。正如你所看到的,我正在使用DOMDocument类。
我的解析 XML 的代码如下所示:
$dom = new DOMDocument();
$metaXML = $dom->load($path."/meta.xml");
Run Code Online (Sandbox Code Playgroud)
我确信路径和一切都是正确的。当我删除前缀时,它工作正常。XML 看起来像:
<meta>
<info gamemodes="race" type="map" edf:represent="false"></info>
</meta>
Run Code Online (Sandbox Code Playgroud)
edf :represent="false"会导致错误。我不想手动删除edf命名空间前缀,因为这不是我想要解析的唯一 XML 文件。它们有数百个,而且数量还在不断增加。
所以,我的问题是,如何忽略此错误(仅适用于 XML 命名空间)或者如何通过DOMDocument类定义/删除命名空间前缀?
我想做这样的事情:
<root:secondlevel:thirdlevel
xmlns:secondlevel="http://secondlevel.com"
xmlns:secondlevel:thirdlevel="http://thirdlevel.com">
</root:secondlevel:thirdlevel>
Run Code Online (Sandbox Code Playgroud)
有没有办法将这些多个级别root:secondlevel:thirdlevel作为有效的 XML 来执行?
所以我确信这是可以解决的,但这只是我通常不需要处理 VBA XML 代码中的 XML 命名空间。所以,我们有一个文件,它实际上是一个名为 SVG 文件Flag_of_the_United_Kingdom.svg,这是文件内容
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 30" width="1200" height="600">
<clipPath id="t">
<path d="M30,15 h30 v15 z v15 h-30 z h-30 v-15 z v-15 h30 z"/>
</clipPath>
<path d="M0,0 v30 h60 v-30 z" fill="#00247d"/>
<path d="M0,0 L60,30 M60,0 L0,30" stroke="#fff" stroke-width="6"/>
<path d="M0,0 L60,30 M60,0 L0,30" clip-path="url(#t)" stroke="#cf142b" stroke-width="4"/>
<path d="M30,0 v30 M0,15 h60" stroke="#fff" stroke-width="10"/>
<path d="M30,0 v30 M0,15 h60" stroke="#cf142b" stroke-width="6"/>
</svg>
Run Code Online (Sandbox Code Playgroud)
这只是一个示例 SVG 文件,但它也适用于其他 SVG 文件,为了 Internet Explorer 呈现文件,它需要根元素上的名称空间属性。 …
有没有办法在 XmlWriter 中使用 WriteStartElement 函数,如下所示:
XmlWriter.WriteStartElement("prefix", "name", null);
Run Code Online (Sandbox Code Playgroud)
发生错误:System.ArgumentException:“无法使用带有空命名空间的前缀。”
我不想在创建元素时设置命名空间 URI。
稍后,当创建其他属性时,我将通过 WriteAttributeString() 添加它。
对于lxml,我不确定如何正确删除现有元素的名称空间并设置新的名称空间。
例如,我正在解析这个最小的 xml 文件:
<myroot xmlns="http://myxml.com/somevalue">
<child1>blabla</child1>
<child2>blablabla</child2>
</myroot>
Run Code Online (Sandbox Code Playgroud)
...我希望它成为:
<myroot xmlns="http://myxml.com/newvalue">
<child1>blabla/child1>
<child2>blablabla</child2>
</myroot>
Run Code Online (Sandbox Code Playgroud)
和lxml:
from lxml import etree as ET
tree = ET.parse('myfile.xml')
root= tree.getroot()
Run Code Online (Sandbox Code Playgroud)
如果我检查root:
In [7]: root
Out[7]: <Element {http://myxml.com/somevalue}myroot at 0x7f6e13832588>
In [8]: root.nsmap
Out[8]: {None: 'http://myxml.com/somevalue'}
In [11]: root.tag
Out[11]: '{http://myxml.com/somevalue}myroot'
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望最终得到:
In [8]: root.nsmap
Out[8]: {None: 'http://myxml.com/newvalue'}
In [11]: root.tag
Out[11]: '{http://myxml.com/newvalue}myroot'
Run Code Online (Sandbox Code Playgroud)
至于标签,只需设置正确的字符串即可。怎么样nsmap?
在开发android的时候,我们在开发布局文件时通常会用到xml。
例子)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
Run Code Online (Sandbox Code Playgroud)
我当时有一个疑问。如何访问http://schemas.android.com/apk/res/android?
因为我查到了一些资料。我知道这不是 URL,而是 URI。所以无法通过互联网访问。那么命名空间信息在哪里呢?如何访问该信息?
我正在尝试从以下XML查询中查询字段(这实际上是一个Web服务调用):
<soap:Envelope xmlns:xsi="[schema]" xmlns:xsd="[shema]" xmlns:soap="[schema]">
<soap:Body>
<RunPackage xmlns="http://tempuri.org/">
<xmlDoc>
<Request>
<SubscriberCode>543253</SubscriberCode>
<CompanyCode>54325</CompanyCode>
<BranchName>TestBranchName</BranchName>
<TempWorksUserName>TempWorksUserName</TempWorksUserName>
[...]
Run Code Online (Sandbox Code Playgroud)
使用以下XML查询:
WITH XMLNAMESPACES('[schema]' AS soap2, DEFAULT '[schema]')
SELECT TransactionID, T2.Loc.query('data(Request/SubscriberCode)') as 'SubscriberCode'
FROM TempWorksRequest
CROSS APPLY RequestXML.nodes('soap2:Envelope/soap2:Body/RunPackage/xmlDoc') as T2(Loc)
Run Code Online (Sandbox Code Playgroud)
它运行但不会返回任何报复!
如果我构建相同的查询,但删除名称空间的东西,那么它的工作原理.例如,以下工作正常:
<xmlDoc> <Request> <SubscriberCode> 543253 </ SubscriberCode> <CompanyCode> 54325 </ CompanyCode> <BranchName> TestBranchName </ BranchName> [...]
SQL查询:
- 为MITS定义命名空间,以便我们可以使用MITS命名空间.WITH XMLNAMESPACES('[schema]'AS soap2,DEFAULT'[schema]')
SELECT TransactionID,T2.Loc.query('data(Request/SubscriberCode)')'SubscriberCode'FROM TempWorksRequest CROSS APPLY RequestXML.nodes('xmlDoc')as T2(Loc)
有任何想法吗?
我有一个格式的XML:
<val:root>
<bla>
<value>1234</value>
</val:root>
Run Code Online (Sandbox Code Playgroud)
我可以将文件的内容添加到$ xml变量中,但是我遇到了"val:root"的问题.
谢谢你的帮助!
编辑:
我尝试了$ xml."val:root",$ xml.val,$ xml.'val:root'和$ xml.{val:root}.在Mizo的回答中找到了解决方案:
$ XML.root .value
在JBOSS服务器启动期间我收到此错误,
19:44:59,307 ERROR [STDERR] 23813 [main] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
19:44:59,307 ERROR [STDERR] org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx]
Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]
19:44:59,307 ERROR [STDERR] at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
19:44:59,307 ERROR [STDERR] at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
19:44:59,307 ERROR [STDERR] at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
19:44:59,307 ERROR [STDERR] at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:281)
19:44:59,307 ERROR [STDERR] at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1294)
19:44:59,307 ERROR [STDERR] at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1287)
19:44:59,307 ERROR [STDERR] at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
Run Code Online (Sandbox Code Playgroud)
这就是我的application.xml的样子,
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> …Run Code Online (Sandbox Code Playgroud) 我有这个POJO,封装了Atom条目的动态非嵌套元素:
public class SimpleElement {
private Namespace namespace;
private String tagName;
private String value;
private Collection<Attribute> attributes;
/* getters/setters/... */
Run Code Online (Sandbox Code Playgroud)
为了完整, Attribute
public class Attribute {
private String name;
private String value;
private Namespace namespace;
/* getters/setters/... */
Run Code Online (Sandbox Code Playgroud)
而且Namespace:
public class Namespace {
private final String uri;
private final String prefix;
/* getters/setters/... */
Run Code Online (Sandbox Code Playgroud)
SimpleElementAdapter将a序列化SimpleElement为其org.w3c.dom.Element对应物.
这种方法的唯一问题是命名空间总是在元素级别结束,而不是在文档根目录.
有没有办法在文档根目录下动态声明名称空间?