我正在尝试使用 StAX API 中的 XMLEventWriter 从头开始编写 XML 文档。
我不知道如何获取要发出的默认名称空间属性。
例如,默认命名空间 URI 字符串为“ http://www.liquibase.org/xml/ns/dbchangelog/1.9 ”。我希望它以 xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9" 的形式出现在我的 XML 根元素中。
这里有什么神奇的配方?XMLEventWriter.setDefaultNamespace() 不起作用。
谢谢,莱尔德
XML 命名空间规范解释说,无前缀属性的解释由它们出现的元素决定。并且无前缀属性名称的命名空间名称始终没有值:
无前缀属性名称的命名空间名称始终没有值。
jid在以下情况下,此规则如何应用于属性的命名空间。
<query xmlns="jabber:iq:roster">
<item jid="romeo@example.com"></item>
</query>
Run Code Online (Sandbox Code Playgroud)
如果命名空间的声明和相关属性都没有前缀,则该属性jid位于命名空间中jabber:iq:roster。
<q:query xmlns:q="jabber:iq:roster">
<q:item q:jid="romeo@example.com"></q:item>
</q:query>
Run Code Online (Sandbox Code Playgroud)
如果命名空间的声明和属性具有相同的前缀,则属性jid也具有命名空间jabber:iq:roster:
<q:query xmlns:q="jabber:iq:roster">
<q:item jid="romeo@example.com"></q:item>
</q:query>
Run Code Online (Sandbox Code Playgroud)
但是,如果使用前缀声明命名空间但该属性没有前缀,则该属性位于哪个命名空间中?我假设该属性jid
具有在父元素中声明的默认名称空间,
<parent xmlns="http://example.com">
<q:query xmlns:q="jabber:iq:roster">
<q:item jid="romeo@example.com"></q:item>
</q:query>
</parent>
Run Code Online (Sandbox Code Playgroud)
或者没有命名空间,如果没有这样的声明。
我理解得对吗?
我构建了第一个 WCF 服务,旨在接收 HTTP Post 请求,这些请求的消息正文采用严格的格式,我无法对其施加影响。这是一个例子:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<AcquirePackagingData xmlns="mydomain.com">
<Account xmlns="http://schemas.microsoft.com/v1.0">
<AssetIdentifier>ergegdgsdyeryegd</AssetIdentifier>
</Account>
</AcquirePackagingData>
</s:Body>
</s:Envelope>
Run Code Online (Sandbox Code Playgroud)
唯一会随每个请求而改变的位是 AssetIdentifier 的值。
我的wcf服务合同如下:
为了让自己能够最大程度地控制 XML,我选择使用 [XmlSerializerFormat] 来使用 XmlSerializer 而不是 DataContractSerializer。
Account 类非常简单: [XmlRoot(Namespace = " http://schemas.microsoft.com/v1.0 ")] [Serialized] public class Account {
private string m_AssetIdentifier;
[XmlElement]
public string AssetIdentifier
{
get { return m_AssetIdentifier; }
set { m_AssetIdentifier = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
当我向此服务发出请求时,就会出现问题。当我使用 WCF 测试客户端时,我可以看到它发出了以下请求:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://schemas.microsoft.com/DRM/2007/03/protocols/ILGIContentProtection/AcquirePackagingData</Action>
</s:Header>
<s:Body>
<AcquirePackagingData xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/DRM/2007/03/protocols">
<challenge xmlns:d4p1="http://schemas.microsoft.com/DRM/2007/03/protocols/AcquirePackagingData/v1.0">
<d4p1:AssetIdentifier>ergegdgsdyeryegd</d4p1:AssetIdentifier>
</challenge> …Run Code Online (Sandbox Code Playgroud) wcf soap xml-namespaces datacontractserializer xmlserializer
我正在学习 XML 和 Web 服务,但我不知道当我的应用程序未连接到互联网时如何获取 XML 命名空间?
我正在使用一个与 XML 命名空间相对应的库,但我不明白它们是如何相关的。
我有以下 xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<bindings xs:schemaLocation="Inbound.wsdl#wsdl:types" node="/schema">
<bindings node="//complexType[@name='TradeIdWithRef']">
<bindings node=".//attribute[@name='_value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//complexType[@name='Bank']">
<bindings node=".//attribute[@name='_value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//complexType[@name='Bid']">
<bindings node=".//attribute[@name='_value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//complexType[@name='addMarketExRequest']">
<bindings node=".//attribute[@name='_value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//complexType[@name='Offer']">
<bindings node=".//attribute[@name='_value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//complexType[@name='Price']">
<bindings node=".//attribute[@name='_value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//complexType[@name='Delta']">
<bindings node=".//attribute[@name='_value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
Run Code Online (Sandbox Code Playgroud)
但是我总是在 xml 验证时收到以下错误:
cvc-elt.1: Cannot find the …Run Code Online (Sandbox Code Playgroud) 我的网站始终通过 HTTPS 提供服务,以保护用户数据等。
但是,我确实有一些带有 xml 命名空间链接的 SVG HTML 标签。这些链接是 HTTP 而不是 HTTPS:
xmlns="http://www.w3.org/2000/svg"
Run Code Online (Sandbox Code Playgroud)
这是一个问题吗?我应该将所有这些设置为 HTTPS 吗?
我找不到信息,如何使用命名空间解析我的 XML:
我有这个xml:
<par:Request xmlns:par="http://somewhere.net/actual">
<par:actual>blabla</par:actual>
<par:documentType>string</par:documentType>
</par:Request>
Run Code Online (Sandbox Code Playgroud)
并试图解析它:
dom = ET.parse(u'C:\\filepath\\1.xml')
rootxml = dom.getroot()
for subtag in rootxml.xpath(u'//par:actual'):
#do something
print(subtag)
Run Code Online (Sandbox Code Playgroud)
并且得到了异常,因为它不知道命名空间前缀。有没有最好的方法来解决这个问题,计算那个脚本不会知道它要解析的文件和标签要搜索的文件?
搜索网络和 stackoverflow 我发现,如果我会在那里添加:
namespace = {u'par': u"http://somewhere.net/actual"}
for subtag in rootxml.xpath(u'//par:actual', namespaces=namespace):
#do something
print(subtag)
Run Code Online (Sandbox Code Playgroud)
那个有效。完美的。但是我不知道我将解析哪个 XML,并且//par:actual我的脚本也不知道搜索标记(例如)。所以,我需要找到从 XML 中提取命名空间的方法。
找了很多方法,如何提取namespace URI,比如:
print(rootxml.tag)
print(rootxml.xpath('namespace-uri(.)'))
print(rootxml.xpath('namespace-uri(/*)'))
Run Code Online (Sandbox Code Playgroud)
但是我应该如何提取前缀来创建 ElementTree 想要的字典?我不想在 xml 正文上使用正则表达式怪物来提取前缀,我相信必须存在支持的方式,不是吗?
也许必须存在一些方法让我通过 ETree 命名空间从 XML 中提取作为字典(如 ETree 想要的!)而无需手动操作?
我正在尝试使用 lxml 解析具有多个命名空间的 XML 文档,但我一直坚持使用 findall() 方法返回某些内容。
我的 XML:
<MeasurementRecords xmlns="http://www.company.com/common/rsp/2012/07"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.company.com/common/rsp/2012/07 RSP_EWS_V1.6.xsd">
<HistoryRecords>
<ValueItemId>100_0000100004_3788_Resource-0.customId_WSx Data Precip Type</ValueItemId>
<List>
<HistoryRecord>
<Value>60</Value>
<State>Valid</State>
<TimeStamp>2016-04-20T12:40:00Z</TimeStamp>
</HistoryRecord>
</List>
</HistoryRecords>
<HistoryRecords>
</MeasurementRecords>
Run Code Online (Sandbox Code Playgroud)
我的代码:
from lxml import etree
from pprint import pprint
RSPxmlFile = '/home/user/Desktop/100_0000100004_3788_20160420144011263_records.xml'
with open (RSPxmlFile, 'rt') as f:
tree = etree.parse(f)
root = tree.getroot()
for node in tree.findall('MeasurementRecords', root.nsmap):
print node
print "parameter = ", node.text
Run Code Online (Sandbox Code Playgroud)
给出:
ValueError: empty namespace prefix is not supported in ElementPath
Run Code Online (Sandbox Code Playgroud)
阅读本文后我尝试了一些实验:
>>> root.nsmap …Run Code Online (Sandbox Code Playgroud) 我正在构建 openoffice 文档。我有一个用于生成 content.xml 文件的脚手架。content-scaffold.xml 文件存储在文件系统中,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content
xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0"
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0"
xmlns:db="urn:oasis:names:tc:opendocument:xmlns:database:1.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
xmlns:grddl="http://www.w3.org/2003/g/data-view#"
xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:odf="http://docs.oasis-open.org/ns/office/1.2/meta/odf#"
xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2"
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:pkg="http://docs.oasis-open.org/ns/office/1.2/meta/pkg#"
xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xlink="http://www.w3.org/1999/xlink"
office:version="1.2">
<office:automatic-styles>
<style:style style:family="text" style:name="Strong">
<style:text-properties
fo:color="#000000"
fo:font-weight="bold" />
</style:style>
</office:automatic-styles>
<office:body>
<office:text>
<!-- content will go here -->
</office:text>
</office:body>
</office:document-content>
Run Code Online (Sandbox Code Playgroud)
这个想法是我把这个 xml 注入到 office:text 标签中(在 python 中),然后将它呈现回来。在这个例子中,我注入了一个简单的 text:p 标签。
document_content = ElementTree.parse('content-scaffold.xml').getroot()
office_body = document_content.find('office:body', NAMESPACES)
office_text = office_body.find('office:text', NAMESPACES) …Run Code Online (Sandbox Code Playgroud) 长话短说,我正在更改应用程序的 Web 服务器容器。旧容器将发送回 SOAP 响应如下:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
... other elements ...
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)
在新服务器中,我可以发回以下响应:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
... other elements ...
</soap:Body>
</soap:Envelope>
Run Code Online (Sandbox Code Playgroud)
如您所见,唯一不同的是使用的命名空间前缀(SOAP-ENV与soap)。我对可能依赖这些 Web 服务的所有客户端都没有透明度,但是这两个 XML 响应是否相同?任何客户端是否会因为看到(只是)根 Envelope 和 Body 标签的不同命名空间前缀而中断,即使它们最终指向相同的 URI?
xml-namespaces ×10
xml ×6
python ×3
elementtree ×2
soap ×2
https ×1
java ×1
lxml ×1
prefix ×1
stax ×1
wcf ×1
web-services ×1
xhtml ×1
xpath ×1
xsd ×1