我有一个来自外部源的XML文档.
<?xml version="1.0" encoding="utf-8"?>
<ns0:Info xmlns:ns0="http://www.ZomboCorp.com/">
<Name>Anthony</Name>
<Job>Developer</Job>
</ns0:Info>
Run Code Online (Sandbox Code Playgroud)
我需要将它反序列化为这样的对象.
public class Info
{
public String Name { get; set; }
public String Job { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
按原样使用,Serializer抛出一个InvalidOperationException
<Info xmlns='http://www.ZomboCorp.com/'>没想到.
如果我添加[XmlElement(Namespace = "http://www.ZomboCorp.com/")]到类定义,则Serializer返回Info具有null属性的新对象.
我正在使用XOM在Java中构建XML文档.
我创建了一个简单的XML文档,我想要一个XML命名空间.但是当我在第一个标签上设置名称空间时,在子项上设置了一个空名称空间xmlns="",如何摆脱这种行为?我只想要xmlns第一个标签.
我想要这个XML:
<request xmlns="http://my-namespace">
<type>Test</type>
<data>
<myData>test data</myData>
</data>
</request>
Run Code Online (Sandbox Code Playgroud)
但这是XOM输出的XML文档
<request xmlns="http://my-namespace">
<type xmlns="">Test</type>
<data xmlns="">
<myData>test data</myData>
</data>
</request>
Run Code Online (Sandbox Code Playgroud)
这是我的Java XOM代码:
String namespace = "http://my-namespace";
Element request = new Element("request", namespace);
Element type = new Element("type");
type.appendChild("Test");
request.appendChild(type);
Element data = new Element("data");
request.appendChild(data);
Element myData = new Element("myData");
myData.appendChild("test data");
data.appendChild(myData);
Document doc = new Document(request);
doc.toXML();
Run Code Online (Sandbox Code Playgroud) 我在使用nekohtml + dom4j解析html文档时遇到了一些问题.
我发现我的xpath表达式不再起作用了,因为最近在html源上添加了一个新的默认html xml命名空间.
规格说:
前缀xmlns仅用于声明名称空间绑定,并且根据定义绑定到名称空间名称 http://www.w3.org/2000/xmlns/.它不能被宣布.其他前缀不得绑定到此命名空间名称,并且不得将其声明为默认命名空间.元素名称不能有前缀xmlns.
但在我的html文档中,最近(我猜)在html标签中添加了:xmlns ="http://www.w3.org/1999/xhtml"
我找到了2个解决方案
1)删除命名空间:
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/namespaces", false);
parser.parse(url);
Run Code Online (Sandbox Code Playgroud)
根据NekoHTML faq的说法.
2)为我的xpath添加一个前缀,绑定到默认的html命名空间.(似乎它不能将前缀"空字符串"绑定到我想要的命名空间)
Map<String,String> XPATH_NAMESPACES = new HashMap<String, String>();
XPATH_NAMESPACES.put("my_prefix", "http://www.w3.org/1999/xhtml");
XPath xpath = document.createXPath(xpathExpr);
xpath.setNamespaceURIs(XPATH_NAMESPACES);
Element element = (Element) xpath.selectSingleNode(document);
Run Code Online (Sandbox Code Playgroud)
然后,我使用// my_prefix:td而不是使用// td作为例子
我发布这些解决方案,因为有些人可能会发现这篇文章很有用.另见http://www.edankert.com/defaultnamespaces.html#Jaxen_and_Dom4J
但我真正想知道的是:
我想我的问题对你们中的一些人来说似乎很明显,但我并没有真正意识到它带来了什么.我已经读过html和xhtml之间的区别.我想使用xhtml dtd的人宁愿使用这个命名空间,但除了它给爬虫或其他类似的东西带来一些额外的痛苦之外,还有什么真正的兴趣?
PS:我已经看到从html传递到xhtml你必须添加xmlns和xml:lang,例如:所以它可能不是我解析的网站的目的,因为没有添加xml:lang ...
谢谢
我有一种感觉,这个问题很简单,但是自从我做了任何xslt以来可能有多年,所以也许有人可以提供帮助?
我有一块由.net类DataContractSerializer生成的xml,我需要使用xslt从这个xml中提取数据,最后得到一些html.对我来说复杂的事情是命名空间的大量使用......
xml的片段如下所示:
<FundDeal xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Deal">
<Id xmlns="http://schemas.datacontract.org/2004/07/Guide.BusinessObjects.Deal">DEAL12345</Id>
<Account xmlns:d2p1="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Account">
<d2p1:AlternateId i:nil="true"/>
<d2p1:Designation>XXX</d2p1:Designation>
<d2p1:Name>QWERTY</d2p1:Name>
<d2p1:Number>12345678</d2p1:Number>
<d2p1:Status i:nil="true"/>
</Account>
<Agent xmlns:d2p1="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Account">
<d2p1:Id>54321</d2p1:Id>
<d2p1:Name>ASDFG</d2p1:Name>
<d2p1:Status>Active</d2p1:Status>
</Agent>
....
</FundDeal>
Run Code Online (Sandbox Code Playgroud)
现在,我需要通过样式表来转换这个xml,并且发现它非常艰难.我认识到xsl需要它自己对所涉及的命名空间的引用,并且可以使用以下xsl轻松地提取上面的Deal Id之类的内容:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt"
xmlns:grbd="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Deal"
xmlns:gbd="http://schemas.datacontract.org/2004/07/Guide.BusinessObjects.Deal"
xmlns:grba="http://schemas.datacontract.org/2004/07/Guide.Rx.BusinessObjects.Account">
<xsl:output indent="yes" omit-xml-declaration="yes" method="html"/>
<xsl:template match="/">
<html>
<head>
<!-- some styles here -->
</head>
<body>
<table cellpadding="5" cellspacing="5" border="0">
<tr>
<td class="SectionTitle" colspan="2">
<xsl:text>Deal Cancellation Notification - </xsl:text>
<xsl:value-of select="//ggbd:Id"/>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
但我正在努力阅读诸如帐户名之类的内容,因为似乎有多个名称空间正在进行中.
任何人都可以告诉我访问的xpath(a)帐户名称,以及(b)代理商名称?我认为看到如何访问这些将可能允许我访问我需要的一切.
非常感谢,皮特
我如何使用jaxb使用以下模式生成xml。
<NS1:getRatesResponse xmlns:NS1="http://mynamespaceTypes">
<response>
<NS2:rates xmlns:NS2="http://mynamespace">
<currency>USD</currency>
</NS2:rates>
<NS3:rates xmlns:NS3="http://mynamespace">
<currency>EUR</currency>
</NS3:rates>
<NS4:rates xmlns:NS4="http://mynamespace">
... etc
</response>
Run Code Online (Sandbox Code Playgroud)
我不知道如何告诉jaxb每个新项目都应该是具有相同名称空间的NS(n + 1)。更改xml格式不是一种选择,因为它是外部的。
JAXB可以正确解析此xml,但是在使用相同的类进行生成时,它会像这样生成:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:getRatesResponse
xmlns:ns2="http://mynamespaceTypes"
xmlns:ns3="http://mynamespace">
<response>
<ns2:rates>
<currency>EUR</currency>
</ns2:rates>
<ns2:rates>
<currency>USD</currency>
</ns2:rates>
</response>
</ns3:getRatesResponse>
Run Code Online (Sandbox Code Playgroud) 我们有一些使用某个命名空间的xml数据,但实际上并没有为它声明命名空间.我们想要对它进行验证,但是默认命名空间中的无效元素不会被捕获,因为xmlnsxml规则没有设置任何内容.在这个处理阶段,文档可能已经加载了很长时间,并且不一定是原始形式(因此命名空间管理器可能是不可能的).
<root>
<valid />
<notvalid />
</root>
Run Code Online (Sandbox Code Playgroud)
var xd = new XmlDocument();
xd.Load(xmlstring);
xd.Validate((sender, args) =>
{
...
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用spring-security为OAuth2设置我们的REST服务器.(服务器已经支持没有OAuth的spring-security).现在我尝试按照sparklr示例并将spring-security-oauth工件添加到我的maven(mvn依赖:树显示它可用)以及命名空间配置到我的spring-security-context.xml但是我得到的只是:
Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security/oauth2]
Run Code Online (Sandbox Code Playgroud)
浏览http://www.springframework.org/schema/security/所有我看到的是.xsd文件但没有oauth2文件夹..怎么会这样?我假设sparklr示例是一个工作版本,所以我做错了什么?
这是我的spring-security-context标题:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2-1.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
Run Code Online (Sandbox Code Playgroud)
如果需要,这是maven设置:
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth</artifactId>
<version>1.0.0.RC2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud) 有什么区别
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ...>
Run Code Online (Sandbox Code Playgroud)
和
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" ...>
Run Code Online (Sandbox Code Playgroud)
以及如何在它们之间切换?
如何更改<SOAP-ENV:Envelope的响应以使用<soap:Envelope?
最近,我一直在使用数据转换工具,该工具使用XSL来修改输入数据的格式.我最近一直遇到命名空间问题,现在我遇到了一个新问题,由上一个问题的解决方案引起.
正确的xmlns存储在父元素中,但第一个子元素(唯一的第一级子节点)包含一个属性xmlns="".我发现了一些类似的问题,但实现的问题/方法不同,足以阻止我直接应用更改.有谁知道如何阻止该属性应用于子数据?我想过要沿着我之前走过的路径(通过序列化XML然后进行字符串操作来修复它),但是所需的序列化功能只存在于xpath 3中,而我使用的转换服务器只支持最多xpath 2,遗憾的是我没有发言权:(
我正在使用Map Force来构建XSL转换,因此,不能简单地编辑XSL(因为它将被map force覆盖),但我相信我可以将XSL更改应用于Map Force.
XSLT的一个小部件
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:core="http://www.altova.com/MapForce/UDF/core" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="core xs fn">
<xsl:template name="core:firstCharacter">
...
</xsl:template>
<xsl:template name="core:tokenize-by-length-internal">
...
</xsl:template>
<xsl:output method="xml" encoding="UTF-8" byte-order-mark="no" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="var1_SwiftMessages" as="node()?" select="SwiftMessages"/>
<xformResult xmlns="urn:...">
<xsl:attribute name="xsi:schemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance" select="'urn:... OutputInterface/xformResult.xsd'"/>
<xformResultRecord>
<xformResultData>
<Document>
<!-- REMAINDER OF FAIRLY STANDARD CODE -->
</Document>
</xformResultData>
</xformResultRecord>
</xformResult>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
这urn:...是对输出文件规范的引用,是输出文件xformResult.xsd的Schema.
然后将变换器XML文件发送回处理程序程序,然后将其中的所有元素<xformResultData>输出到文件中.这就是问题所在.输出文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:..." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Cstmr xmlns="">
<!-- …Run Code Online (Sandbox Code Playgroud) 我试图定义一个可以在后续元素定义中引用的属性.
<xs:attribute name="ridref" type="xs:string"/>
Run Code Online (Sandbox Code Playgroud)
后来我像这样使用它:
<xs:element name="coordRegRef">
<xs:complexType>
<!--xs:attribute name="ridref" type="xs:string"/ this works but I want to use ref -->
<xs:attribute ref="ridref" use="required"/>
</xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
XSD使用xmllint编译好
xmllint --schema pc-ar.xsd pc-ar.xml
Run Code Online (Sandbox Code Playgroud)
但是xmllint说
pc-ar.xml:41: element coordRegRef: Schemas validity error : Element '{http://pc-ar.xsd}coordRegRef', attribute 'ridref': The attribute 'ridref' is not allowed.
pc-ar.xml:41: element coordRegRef: Schemas validity error : Element '{http://pc-ar.xsd}coordRegRef': The attribute '{http://pc-ar.xsd}ridref' is required but missing.
Run Code Online (Sandbox Code Playgroud)
我解释为我的XML文件必须使用ridref的命名空间
<coordRegRef fix:ridref="111"/>
Run Code Online (Sandbox Code Playgroud)
(这确实有效,但不受欢迎)而不是
<coordRegRef ridref="111"/>
Run Code Online (Sandbox Code Playgroud)
为什么?
我的XSD
<?xml version="1.0"?>
<!-- validate: xmllint - -schema pc-ar.xsd …Run Code Online (Sandbox Code Playgroud) xml-namespaces ×10
xml ×8
namespaces ×3
java ×2
xslt ×2
.net ×1
c# ×1
html ×1
jaxb ×1
oauth-2.0 ×1
php ×1
soap ×1
spring ×1
transform ×1
validation ×1
xmldocument ×1
xmllint ×1
xom ×1
xsd ×1