E4X:抓取带有命名空间的节点?

发布于 2024-07-21 20:21:05 字数 460 浏览 9 评论 0原文

我想学习如何在 E4X 中使用命名空间处理 XML,所以基本上这就是我想学习的内容,假设我有一些像这样的 XML:

<rdf:RDF 
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns="http://purl.org/rss/1.0/">
    <rdf:item value="5"/>
    <item value="10"/>
</rdf:RDF>

How can I allocate to一个名为 rdfItems 的 var 和 到一个名为 regItems 的 var?

谢谢!

I want to learn how to process XML with namespaces in E4X so basically here is what I want to learn, say I have some XML like this:

<rdf:RDF 
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns="http://purl.org/rss/1.0/">
    <rdf:item value="5"/>
    <item value="10"/>
</rdf:RDF>

How could I assign <rdf:item/> to a var called rdfItems and <item/> to a var called regItems?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭水袖舞倾城 2024-07-28 20:21:05

如果您有一个包含多个名称的 XML,但在从 XML 获取值时您不关心名称空间,您可以执行以下操作...

示例 XML

<ns1:Item>
  <ns1:ItemType>Printed Material</ns1:ItemType>
  <ns2:Book isbn="123456">
    <ns2:Author>
      <ns2:FirstName>James</ns2:FirstName>
      <ns2:LastName>Smith</ns2:LastName>
    </ns2:Author>
    <ns2:Title>The Book Title</ns2:Title>
  </ns2:Book>
<ns1:Item> 

您可以获取任何项目,而不管名称空间如何,如下所示

var itemType:String = xml.*::ItemType;
var bookISBN:Number = xml.*::Book.@isbn;
var bookTitle:String = xml.*::Book.Title;

If you have a XML that contains multiple names but you don't care about the namespaces when getting values from the XML you can do the following....

Example XML

<ns1:Item>
  <ns1:ItemType>Printed Material</ns1:ItemType>
  <ns2:Book isbn="123456">
    <ns2:Author>
      <ns2:FirstName>James</ns2:FirstName>
      <ns2:LastName>Smith</ns2:LastName>
    </ns2:Author>
    <ns2:Title>The Book Title</ns2:Title>
  </ns2:Book>
<ns1:Item> 

You could get any item regardless of namespace like this

var itemType:String = xml.*::ItemType;
var bookISBN:Number = xml.*::Book.@isbn;
var bookTitle:String = xml.*::Book.Title;
辞取 2024-07-28 20:21:05

我不确定这是否准确地回答了问题,但考虑到您的情况,以下代码将检索这两个值(考虑到下面引用的“xml”变量是一个包含您提供的 XML 代码片段的 XML 对象)

// Your "rdf" namespace
namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
use namespace rdf;

// Your "reg" (i.e, default) namespace
namespace reg = "http://purl.org/rss/1.0/";
use namespace reg;

private function getYourValues():void
{               
    var rdfitems:String = xml.rdf::item.@value;
    var regitems:String = xml.reg::item.@value;
}

:需要在“rdf”项和“non-rdf”项之间进行创建,因为它们的元素名称在其他方面是相同的,因此声明第二个命名空间以允许您独立检索每个项。 希望能帮助到你!

I'm not sure whether this answers the question exactly, but given your scenario, the following code retrieves both values (given the "xml" variable, referenced below, is an XML object containing the snippet of XML code you provided):

// Your "rdf" namespace
namespace rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
use namespace rdf;

// Your "reg" (i.e, default) namespace
namespace reg = "http://purl.org/rss/1.0/";
use namespace reg;

private function getYourValues():void
{               
    var rdfitems:String = xml.rdf::item.@value;
    var regitems:String = xml.reg::item.@value;
}

A distinction needs to be made between the "rdf" item and the "non-rdf" one, since their element names are otherwise identical, so the second namespace is declared to allow you to retrieve each item independently. Hope it helps!

多情出卖 2024-07-28 20:21:05

如果您不知道必须处理的名称空间,可以使用多种方法。

node.namespace().prefix     //returns prefix i.e. rdf
node.namespace().uri        //returns uri of prefix i.e. http://www.w3.org/1999/02/22-rdf-syntax-ns#

node.inScopeNamespaces()   //returns all inscope namespace as an associative array like above

//returns all nodes in an xml doc that use the namespace
var nsElement:Namespace = new Namespace(node.namespace().prefix, 
node.namespace().uri);

var usageCount:XMLList = node..nsElement::*;

你最好的选择就是尝试一下。 但我确实喜欢过滤 xml 的前置逻辑语句,使其更容易使用。

希望这能给您一些动态处理命名空间

的想法,

Jon

If you don't know the namespace you have to deal with there are a variety of methods you can use.

node.namespace().prefix     //returns prefix i.e. rdf
node.namespace().uri        //returns uri of prefix i.e. http://www.w3.org/1999/02/22-rdf-syntax-ns#

node.inScopeNamespaces()   //returns all inscope namespace as an associative array like above

//returns all nodes in an xml doc that use the namespace
var nsElement:Namespace = new Namespace(node.namespace().prefix, 
node.namespace().uri);

var usageCount:XMLList = node..nsElement::*;

your best bet is to just play around with it. But i do like the prediate logic statement to filter the xml make it much easier to work with.

hope this give you some ideas to dynamically handle namespaces

regards,

Jon

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文