标签: xml-namespaces

使用 perl 的 XML::LibXML 如何使用 XML Prefixes 而不是 xmlns 属性?

我相信这个问题可能以前曾在 2006 年在不同的网站上尝试过。但是,我当前的 XML/RDF 编写器 ( XML::LibXML1.70) 以 xmlns 属性的形式输出元素名称空间。这将排除使用非命名空间感知解析器的人,他们只对foaf:Person. 我想知道是否有人知道在 perl 中实现这一目标的简单方法,首先是使用XML::LibXML. 或者通过不同的方式。

像这样的节点:

  <Person xmlns="http://xmlns.com/foaf/0.1/" rdf:ID="me"/>
Run Code Online (Sandbox Code Playgroud)

和这个:

  <name xmlns="http://xmlns.com/foaf/0.1/">Evan Carroll</name>
Run Code Online (Sandbox Code Playgroud)

应该看起来像:

  <foaf:Person rdf:ID="me"/>
  <foaf:name>Evan Carroll</name>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我相信无论哪种方式在技术上都是正确的,但我宁愿不依赖其他人知道这一点。昨天我自己也不知道。

xml perl xml-namespaces libxml2

2
推荐指数
1
解决办法
2360
查看次数

如何使用XDocument.XPathEvaluate处理空命名空间?

我正在尝试使用XDocument和XPathEvaluate从woot.com提要中获取值.我正在处理其他名称空间,但这个例子给了我一些问题.

     <rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
      <channel>    
        <category text="Comedy" xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd">
</category> 
<!-- this is a problem node, notice 'xmlns=' --!>
Run Code Online (Sandbox Code Playgroud)

所以我试试这个:

  XmlNamespaceManager man = new XmlNamespaceManager(nt);
    man.AddNamespace("ns", "http://www.w3.org/2000/xmlns/");
// i've also tried man.AddNamespace("ns", string.Empty);
    xDocument.Namespace = man;
    var val = xDocument.XPathEvaluate("/rss/channel/ns:category/@text", xDocument.Namespace);
Run Code Online (Sandbox Code Playgroud)

val始终为null.我正在使用ns:来自VS 2010 XPath Navigator插件的建议.有关如何处理这个问题的任何想法?

.net xpath linq-to-xml xml-namespaces

2
推荐指数
1
解决办法
1209
查看次数

如何使用XMLReader读取带有未定义命名空间的XML文件?

我对解析XML文件相对较新,并且正在尝试使用XMLReader读取大型XML文件.

<?xml version="1.0" encoding="UTF-8"?>
<ShowVehicleRemarketing environment="Production" lang="en-CA" release="8.1-Lite" xsi:schemaLocation="http://www.starstandards.org/STAR /STAR/Rev4.2.4/BODs/Standalone/ShowVehicleRemarketing.xsd">
  <ApplicationArea>
    <Sender>
      <Component>Component</Component>
      <Task>Task</Task>
      <ReferenceId>w5/cron</ReferenceId>
      <CreatorNameCode>CreatorNameCode</CreatorNameCode>
      <SenderNameCode>SenderNameCode</SenderNameCode>
      <SenderURI>http://www.example.com</SenderURI>
      <Language>en-CA</Language>
      <ServiceId>ServiceId</ServiceId>
    </Sender>
    <CreationDateTime>CreationDateTime</CreationDateTime>
    <Destination>
      <DestinationNameCode>example</DestinationNameCode>
    </Destination>
  </ApplicationArea>
...
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

ErrorException [警告]:XMLReader :: read()[xmlreader.read]:compress.zlib:// D:/WebDev/example/local/public/../upload/example.xml.gz:2:名称空间错误:未定义ShowVehicleRemarketing上schemaLocation的命名空间前缀xsi

我一直在搜索并找不到有关使用XMLReader读取带有命名空间的XML文件的更多有用信息 - 我如何定义命名空间,如果这实际上是我需要做什么...帮助不大?链接到相关资源?

php xml parsing xmlreader xml-namespaces

2
推荐指数
1
解决办法
7549
查看次数

如何忽略xml命名空间?

我有一个看起来像这样的测试xml文件:

<Person>
  <ContactInfo>
   ...
  <ContactInfo>
</Person>
Run Code Online (Sandbox Code Playgroud)

当我尝试反序列化时,一切正常。但问题是有时这个xml文件的结构不同——有时添加xml命名空间。

<Person xmlns:xsd="http://www.w3.org/2001/XMLSchema"    
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <ContactInfo>
   ...
  <ContactInfo>
</Person>
Run Code Online (Sandbox Code Playgroud)

现在,当我进行序列化时,我收到 IOnvalidOperationException:“XML 文档 (1, 2) 中存在错误”。内部异常消息说<Person xmlns='http://tempuri.org/PaymentInformationXml.xsd'>不是预期的。

那么有人可以帮我解决这个问题吗?

.net xml serialization xml-namespaces deserialization

2
推荐指数
1
解决办法
9076
查看次数

如何将xmlns属性添加到现有的xml文档中

我有现有的xml文档.

例如

<Test>
  <A />
</Test>
Run Code Online (Sandbox Code Playgroud)

我将此xml加载到XDocument中.我需要将属性xmlns添加到此文档并使用此属性保存它.

var xml = new XDocument.Load("c:\\filePath.xml");
Run Code Online (Sandbox Code Playgroud)

我在尝试这个时:

xml.Root.SetAttributeValue("xmlns", "http://namespaceuri");
Run Code Online (Sandbox Code Playgroud)

我得到例外:

System.Xml.XmlException: The prefix '' cannot be redefined from 'http://namespaceuri' to  within the same start element tag.
Run Code Online (Sandbox Code Playgroud)

谢谢

c# linq-to-xml xml-namespaces

2
推荐指数
1
解决办法
5085
查看次数

使用冒号 (:) 读取 XML

我正在尝试从以下 XML 文档 ( https://gdata.youtube.com/feeds/api/videos?q=example )获取视频的视图,我能够获取链接和作者,因为标签中没有冒号。

我正在尝试获取 yt:statistics 但我不知道如何获取。

    result = e.Result.Replace("xmlns='http://www.w3.org/2005/Atom' ", String.Empty);

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(result);

    XmlNodeList videos = doc.GetElementsByTagName("entry");

    foreach (XmlNode video in videos)
    {
        XmlNode insideauthor = video.SelectSingleNode("author");

        string videoId = video.SelectSingleNode("id").InnerText.Replace("http://gdata.youtube.com/feeds/api/videos/", String.Empty);
        string author = insideauthor.SelectSingleNode("name").InnerText;

        // Trying to get the views of a video of the search results
        MessageBox.Show(video.SelectSingleNode("yt:statistics").Attributes["viewCount"].InnerText);
    }
Run Code Online (Sandbox Code Playgroud)

c# xmldocument xml-namespaces selectsinglenode

2
推荐指数
1
解决办法
3088
查看次数

具有命名空间的元素的 XPath

newVersion元素中 属性的 XPATH 是什么

<dependentAssembly>
<assemblyIdentity name="System.Reactive.Linq" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
</dependentAssembly> 
Run Code Online (Sandbox Code Playgroud)

我已经尽力自己做。但是不知道如何为具有命名空间的元素获取 XPATH。它非常混乱。有人请给我一个XPATH。

我想出的 XPATH 是

/configuration/runtime/assemblyBinding/dependentAssembly[2]/bindingRedirect[@newVersion='2.2.5.0']/@newVersion


<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="1" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Reactive.Interfaces" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect name="Test1" oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Reactive.Linq" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.5.0" newVersion="2.2.5.0" />
  </dependentAssembly>      
</assemblyBinding>
Run Code Online (Sandbox Code Playgroud)

msbuild xpath xml-namespaces

2
推荐指数
1
解决办法
1951
查看次数

使用 vbscript 创建 XML 元素时如何避免 xmlns 属性?

添加新元素时,我看到xmlns添加了空字符串的属性。我怎样才能避免这种情况?我看到的答案很少,但它们要么是 Java 要么是 .Net。仍然尝试了这些,但它们不起作用。我需要一个 VBScript 的解决方案。

'load the xml file
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")  
objXMLDoc.load(strFilePath)

'get all <MainError> nodes in the xml
Set mainNode = objXMLDoc.documentElement.SelectNodes("//MainError")

'get child nodes for the first <MainError> node
Set childNodes = mainNode(0).ChildNodes

Set objErrorNode = objXMLDoc.createElement("ChildError")
objErrorNode.text = "somevalue"
mainNode(0).appendChild(objErrorNode)
Run Code Online (Sandbox Code Playgroud)

输出:

<MainError><ChildError xmlns="">somevalue</ChildError></MainError>
Run Code Online (Sandbox Code Playgroud)

xml vbscript xml-namespaces xmldom

2
推荐指数
1
解决办法
2205
查看次数

SQL Server 子节点中的空白 xml 命名空间

您好,我需要 sql 中以下内容的帮助:

我需要创建一个这种格式的xml文件

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04">
 <FIToFIPmtStsRpt>
  <GrpHdr>
    <MsgId></MsgId>
  </GrpHdr>
  <OrgnlGrpInfAndSts>
    <OrgnlMsgId />
  </OrgnlGrpInfAndSts>
 </FIToFIPmtStsRpt>
</document>
Run Code Online (Sandbox Code Playgroud)

目前我有一个保存主要信息的变量,并且我建立了信息之间的关系(假设 grphdr 可以使用不同的信息多次插入到主 xml 中)

declare @xml xml='<Document
xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04">
<FIToFIPmtStsRpt>
</FIToFIPmtStsRpt>
</Document>
'

declare @xmlgrp xml='<GrpHdr>
  <MsgId></MsgId>
</GrpHdr>'
--here i do some code to fill msgid
Run Code Online (Sandbox Code Playgroud)

然后当我将 grphdr 添加回主 xml 时

SET @xml.modify
('declare namespace a= "urn:iso:std:iso:20022:tech:xsd:001.002.001.04";
insert sql:variable("@xmlgrp") 
into (a:Document/a:FIToFIPmtStsRpt)[1]')

select @xml
Run Code Online (Sandbox Code Playgroud)

我需要找出顶级文件格式,但现在发生的情况如下

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:001.002.001.04">
 <FIToFIPmtStsRpt>
   <GrpHdr xmlns="">-- i need this xmlns tag out
     <MsgId />
   </GrpHdr>
 </FIToFIPmtStsRpt>
 </Document>
Run Code Online (Sandbox Code Playgroud)

不知怎的,我需要 xml 中的空 xmlns …

xml sql t-sql sql-server xml-namespaces

2
推荐指数
1
解决办法
1941
查看次数

具有命名空间的 PowerShell XML 选择节点

我正在尝试从 XML 文件中选择节点,但遇到了似乎由命名空间引起的问题。

下面的代码不返回任何内容。但是如果我从 XML 文件中删除命名空间,我会得到预期的结果。

移动电源

$StandaloneXML = "test.xml"
# Load XML content
$NewStandaloneXML = New-Object -TypeName "System.XML.XMLDocument"
$NewStandaloneXML.Load($StandaloneXML)
# Get namespace
$Namespace = New-Object -TypeName "Xml.XmlNamespaceManager" -ArgumentList $NewStandaloneXML.NameTable
$Namespace.AddNamespace("jboss", $NewStandaloneXML.DocumentElement.NamespaceURI)
$NewStandaloneXML.SelectNodes("jboss:server/interfaces/interface", $Namespace)
Run Code Online (Sandbox Code Playgroud)

XML

<?xml version="1.0" ?>
<server xmlns="urn:jboss:domain:4.2">
  <interfaces>
      <interface name="management">
          <inet-address value="${jboss.bind.address.management:127.0.0.1}"/>
      </interface>
      <interface name="public">
          <inet-address value="${jboss.bind.address:127.0.0.1}"/>
      </interface>
  </interfaces>
</server>
Run Code Online (Sandbox Code Playgroud)

预期输出

名称 inet 地址
---- ------------
管理 inet-address
公网地址

xml powershell xpath namespaces xml-namespaces

2
推荐指数
1
解决办法
3224
查看次数