好吧,我已经看到了很多关于这个问题的变化,但没有一个完全回答我想要解决的问题,也许我只是太过密集而无法看到如何将其他答案应用于我正在尝试做的事情.
我有一些类似于以下内容的XML:
<?xml version="1.0" encoding="utf-8"?>
<message>
<cmd id="api_info">
<api-version>1.0</api-version>
<api-build>1.0.0.0</api-build>
</cmd>
</message>
Run Code Online (Sandbox Code Playgroud)
现在我有一个XSLT转换,我正在应用于这个XML.XSLT类似于以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
version="2.0">
<xsl:output method="xml" version="1.0" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="message"/>
</xsl:template>
<xsl:template match="message">
<xsl:element name="message" xmlns="http://www.companyname.com/schemas/product/Version001">
<xsl:apply-templates select="/message/cmd/@id"/>
</xsl:element>
</xsl:template>
<xsl:template match="/message/cmd/@id">
<xsl:variable name="_commandType" select="/message/cmd/@id"/>
<xsl:element name="messageHeader">
<xsl:element name="cmdType">
<xsl:value-of select="$_commandType"/>
</xsl:element>
</xsl:element>
<xsl:element name="messageBody">
<xsl:choose>
<xsl:when test="$_commandType = 'api_info'">
<xsl:element name="apiInfoBody">
<xsl:element name="apiVersion">
<xsl:value-of select="/message/cmd/api-version"/>
</xsl:element>
<xsl:element name="apiBuild">
<xsl:value-of select="/message/cmd/api-build"/>
</xsl:element>
</xsl:element>
</xsl:when>
<xsl:when test="$_commandType = 'communicationError'">
<xsl:element name="communicationErrorBody"> …Run Code Online (Sandbox Code Playgroud) 当xml路径名称空间前缀设置时,我无法获得以下xpath表达式.
/bk:BookStore/bk:Books/bk:Book[text()='Time Machine']
XML是:
<BookStore xmlns:bk="http://www.bookstore.com/book#">
<bk:Books>
<bk:Book id="1">Time Machine></bk:Book>
</bk:Books>
</bk:BookStore>
Run Code Online (Sandbox Code Playgroud) 我正在使用Linq to XML来创建一个新的XML文件.我从现有的XML文件中获取文件的某些部分.我使用以下代码.
var v2 = new XDocument(
new XDeclaration("1.0", "utf-16", ""),
new XComment(string.Format("Converted from version 1. Date: {0}", DateTime.Now)),
new XElement(ns + "keyem",
new XAttribute(XNamespace.Xmlns + "xsd", xsd.NamespaceName),
new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
new XAttribute(xsi + "schemaLocation", schemaLocation.NamespaceName),
new XAttribute("version", "2"),
new XAttribute("description", description),
new XElement(ns + "layout",
new XAttribute("type", type),
new XAttribute("height", height),
new XAttribute("width", width),
settings.Root) // XML from an existing file
Run Code Online (Sandbox Code Playgroud)
问题是它添加了xmlns =""现有文件中的第一个元素.
结果是:
<?xml version="1.0" encoding="utf-16"?>
<foo
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema.xsd"
xmlns="http://tempuri.org/KeyEmFileSchema.xsd">
<settings xmlns="">
... …Run Code Online (Sandbox Code Playgroud) 我有以下代码
DocumentBuilderFactory dbFactory_ = DocumentBuilderFactory.newInstance();
Document doc_;
DocumentBuilder dBuilder = dbFactory_.newDocumentBuilder();
StringReader reader = new StringReader(s);
InputSource inputSource = new InputSource(reader);
doc_ = dBuilder.parse(inputSource);
doc_.getDocumentElement().normalize();
Run Code Online (Sandbox Code Playgroud)
然后我就能做到
doc_.getDocumentElement();
Run Code Online (Sandbox Code Playgroud)
得到我的第一个元素,但问题是而不是job元素是tns:job.
我知道并试图使用:
dbFactory_.setNamespaceAware(true);
Run Code Online (Sandbox Code Playgroud)
但这不是我正在寻找的东西,我需要一些东西来完全摆脱命名空间.
任何帮助将不胜感激,谢谢,
玩笑
我有一个包含Web服务的ASP.NET Web项目.当我运行该服务时,它会将我带到一个页面,显示所有暴露的方法,使用类似的URL http://api.example.com/game/service.asmx.
在Web Service的代码中,有些方法具有以下属性:
[WebService(Namespace = "http://webservices.example.com/GameServices/Game1")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Game1 : System.Web.Services.WebService
{
// code
}
Run Code Online (Sandbox Code Playgroud)
我对为什么带有webService属性的类上的命名空间与Web服务的路径不同有点困惑.该命名空间来自哪里?它刚刚组成吗?
我有一些XML:
<?xml version="1.0" ?>
<Project ToolsVersion="4.0">
<PropertyGroup Condition="'$(key)'=='1111'">
<Key>Value</Key>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
注意:这不是我正在使用的实际XML,它只是更漂亮和更短,并演示了这个问题.
使用MSXML我可以查询节点:
IXMLDOMNode node = doc.selectSingleNode("//PropertyGroup/@Condition");
Run Code Online (Sandbox Code Playgroud)
它工作正常:
条件= " '$(密钥)' == '1111'"
实际上,我的XML包含一个名称空间声明:
的xmlns = "http://schemas.microsoft.com/developer/msbuild/2003"
制作实际的 XML文档:
<?xml version="1.0" ?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(key)'=='1111'">
<Key>Value</Key>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
现在我的查询:
IDOMNode node = doc.selectSingleNode("//PropertyGroup/@Condition");
Run Code Online (Sandbox Code Playgroud)
不返回匹配的节点.
如何使用MSXML查询默认命名空间?
注意:
我已经知道如何查询xml中的非默认命名空间 ; 你用:
doc.setProperty("SelectionNamespaces",
"xmlns="http://schemas.microsoft.com/developer/msbuild/2003");
Run Code Online (Sandbox Code Playgroud)我已经知道如何查询.NET中的默认命名空间.您使用命名空间管理器,为默认命名空间命名,然后使用该名称进行查询,然后您可以查询非默认命名空间,因为它不再是默认命名空间
我可以xmlns从我收到的XML字符串中删除令人反感的文本,但我宁愿"以正确的方式去做"
如何使用MSXML 查询"默认"或"未命名"命名空间?
注意:实际上我正在使用SQL Server的XML ShowPlan输出:
<?xml version="1.0" …Run Code Online (Sandbox Code Playgroud) 我有一个很大的WPF应用程序,有很多XAML文件.每个XAML文件都有5到10个clr到xml命名空间映射xmlns:abc="clr-namespace:Abcdef".
它看起来很糟糕,并且在每个文件中写入都很痛苦.
有没有办法在全球范围内定义?
在Oracle论坛,Stackoverflow,java.net上阅读了有关此内容的所有帖子后,我终于在这里发帖了.我正在使用JAXB来创建XML文件,但问题是它在我的元素之前添加了着名的ns2前缀,我已经尝试了所有没有人为我工作的解决方案.java -version给出"1.6.0_37"
解决方案1:使用package-info.java
我在我的包中创建了包含带有以下内容的@ Xml*注释类的文件:
@XmlSchema(
namespace = "http://mynamespace",
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns = {
@XmlNs(namespaceURI = "http://mynamespace", prefix = "")
}
)
package com.mypackage;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Run Code Online (Sandbox Code Playgroud)
解决方案2:NamespacePrefixMapper
我创建了以下类并将映射器设置为marshaller:
// Change mapper to avoid ns2 prefix on generated XML
class PreferredMapper extends NamespacePrefixMapper {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return "";
}
}
NamespacePrefixMapper mapper = new PreferredMapper();
try {
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
}
catch (PropertyException e) {
logger.info("No property …Run Code Online (Sandbox Code Playgroud) 我试过了:
textBox1.Text = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("root1", new XAttribute( "xmlns", @"http://example.com"), new XElement("a", "b"))
).ToString();
Run Code Online (Sandbox Code Playgroud)
但我得到:
The prefix '' cannot be redefined from '' to 'http://example.com' within the same start element tag.
Run Code Online (Sandbox Code Playgroud)
我也试过替换(根据我发现的答案):
XAttribute(XNamespace.Xmlns,...
Run Code Online (Sandbox Code Playgroud)
但也有错误.
注意:我不是要在文档中使用多个xmlns.
xml-namespaces ×10
xml ×4
c# ×3
linq-to-xml ×2
namespaces ×2
.net ×1
asmx ×1
dom ×1
domdocument ×1
fbml ×1
html5 ×1
java ×1
jaxb ×1
linq ×1
msxml ×1
msxml6 ×1
prefix ×1
web-services ×1
wpf ×1
xaml ×1
xhtml ×1
xpath ×1
xslt ×1