E4X:解析具有命名空间节点和要评估的变量的文档?

发布于 2024-08-07 02:43:13 字数 478 浏览 10 评论 0原文

我正在尝试解析具有如下所示节点的文档:

<doc>
<results>
        <result xmlns="http://www.inktomi.com/">
            <title>Senate Panel to Review Election Unit Sale</title>
        </result>
</result>
</doc>

但是结果的名称空间和节点名可能不同。如果不是这样,这会起作用:

results..*::title //>Senate Panel to ...

但这样做不行:

var myvar = "title"
results..*::[myvar] 

有任何线索吗?

I am trying to parse a document with nodes that look something like this:

<doc>
<results>
        <result xmlns="http://www.inktomi.com/">
            <title>Senate Panel to Review Election Unit Sale</title>
        </result>
</result>
</doc>

However the namespace and the nodename of the result could be different. If it were not so, this would work:

results..*::title //>Senate Panel to ...

but doing this doesnt:

var myvar = "title"
results..*::[myvar] 

any clues?

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

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

发布评论

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

评论(3

梦途 2024-08-14 02:43:13

所以正确的解决方案显然是:

var myvar = "title"
var ans = results..*.(localName()==myvar);

感谢 Twitter 上的 @xtyler 找到了答案

So the correct solution apparently is:

var myvar = "title"
var ans = results..*.(localName()==myvar);

Thanks to @xtyler on Twitter for finding the answer

初见你 2024-08-14 02:43:13

显然,访问子级的方括号方式和使用 * 选择任何命名空间不能一起工作

var doc:XML = 
<doc> 
    <results> 
        <result xmlns="http://www.inktomi.com/">  
            <title>Senate Panel to Review Election Unit Sale</title> 
        </result>
    </results>
</doc>;
var ns:Namespace = new Namespace("http://www.inktomi.com/");
trace(doc..*::title.toXMLString()); //These three 
trace(doc.results.*::result);       //lines compile
trace(doc.results.ns::["result"]);  //and run as expected
//This commented out line compiles but throws 2 verify errors in the run time
//trace(doc.results.*::["result"]); 

VerifyError:错误 #1080:命名空间值非法。
ReferenceError:错误#1065:未定义变量测试。

VerifyError 类表示当 SWF 格式错误或损坏时发生的错误遇到文件。

Apparently the square bracket way of accessing children and use of * to select any namespace doesn't work together

var doc:XML = 
<doc> 
    <results> 
        <result xmlns="http://www.inktomi.com/">  
            <title>Senate Panel to Review Election Unit Sale</title> 
        </result>
    </results>
</doc>;
var ns:Namespace = new Namespace("http://www.inktomi.com/");
trace(doc..*::title.toXMLString()); //These three 
trace(doc.results.*::result);       //lines compile
trace(doc.results.ns::["result"]);  //and run as expected
//This commented out line compiles but throws 2 verify errors in the run time
//trace(doc.results.*::["result"]); 

VerifyError: Error #1080: Illegal value for namespace.
ReferenceError: Error #1065: Variable Test is not defined.

The VerifyError class represents an error that occurs when a malformed or corrupted SWF file is encountered.

浅听莫相离 2024-08-14 02:43:13

不是 E4X 解决方案,但您可以迭代 xml.namespaceDeclarations() 返回的所有可用命名空间,然后获取第一个子级或使用方括号来访问它。

您也可以只预先解析 xml 并使所有名称空间相同作为快速修复。

Not an E4X solution, but you could iterate through all available namespaces returned by xml.namespaceDeclarations() then either get the first child or use square brackets to access it.

You could also just pre-parse the xml and make all the namespaces the same as a quick fix.

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