如何使用 idref 解析 xml 元素

发布于 2024-12-02 07:22:18 字数 1084 浏览 1 评论 0 原文

给定一些示例 data.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <categories>
        <category id="google">
            <name>Google</name>
        </category>
    <categories>
    <display>
        <categories>
            <category idref="google"/>
        </categories>
    </display>
</data>

以及一些用于获取 data.xml 文件的 jquery 代码:

$.ajax( {
    url: '/data.xml',
    dataType: 'xml',
    success: function( data )
    {
        $data = $( data );
        // fetch categories to display
        $categories = $data.find( 'display > categories > category' );
    }
} );

什么是解析 category 的高效且紧凑的方法 $categories 中获取的元素通过其 idref 属性引用的 code> 元素?

我想出了如下的方法:

$categories.each( function() {
    var $category = $data.find( 'categories > category[id=' + $( this ).attr( 'idref' ) + ']' );
} );

但我认为可能有一种更紧凑的方式来收集元素。

Given some sample data.xml file:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <categories>
        <category id="google">
            <name>Google</name>
        </category>
    <categories>
    <display>
        <categories>
            <category idref="google"/>
        </categories>
    </display>
</data>

And some jquery code to fetch the data.xml file:

$.ajax( {
    url: '/data.xml',
    dataType: 'xml',
    success: function( data )
    {
        $data = $( data );
        // fetch categories to display
        $categories = $data.find( 'display > categories > category' );
    }
} );

What is a efficient and compact method to resolve the category elements to which the fetched elements in $categories refer to by their idref attribute?

I've come up with something like the following:

$categories.each( function() {
    var $category = $data.find( 'categories > category[id=' + $( this ).attr( 'idref' ) + ']' );
} );

But I thought there might be a more compact way of collecting the elements.

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

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

发布评论

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

评论(2

残疾 2024-12-09 07:22:18

您可以从 idref 属性创建 id 选择器

var referenced = $.unique($categories.map(function() {
    var $found = $data.find("#" + $(this).attr("idref"));
    return ($found.length ? $found[0] : null);
}).get());

上面的代码使用 map()$.unique() 构建一个包含所有引用 元素的唯一实例的数组。

You can create id selectors from the idref attributes:

var referenced = $.unique($categories.map(function() {
    var $found = $data.find("#" + $(this).attr("idref"));
    return ($found.length ? $found[0] : null);
}).get());

The code above uses map() and $.unique() to build an array containing unique instances of all the referenced <category> elements.

沧桑㈠ 2024-12-09 07:22:18
var $data = $( data );
var $byId = {};

$("*[id]", $data).each(function () {
  var $this = $(this);
  $byId[ $this.attr("id") ] = $this;
});

// later...
$byId["google"].find("name");  //  <name>Google</name>
var $data = $( data );
var $byId = {};

$("*[id]", $data).each(function () {
  var $this = $(this);
  $byId[ $this.attr("id") ] = $this;
});

// later...
$byId["google"].find("name");  //  <name>Google</name>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文