如何使用 XPath 选择非空同级节点的笛卡尔积集? [英] How to use XPath to select Cartesian Product set of non null sibling nodes?

查看:20
本文介绍了如何使用 XPath 选择非空同级节点的笛卡尔积集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次在这里发帖,所以如果我做错了什么,请不要犹豫让我知道.

First time poster here, so please don't hesitate to let me know if I've done something wrong.

我正在使用 Altova Stylevision 打印表单文档,我需要为每个唯一的两个节点集合集打印一份表单副本.下面是一个示例 XML,以更好地解释我的要求.

I'm using Altova Stylevision to print out form documents, and I need to print a copy of the form for each unique set of two node collections. Below is a sample XML to better explain what I'm asking for.

<form>
    <Animals>
        <Animal>
            <AName>Boomer</AName>
            <AAddress>House</AAddress>
        </Animal>
        <Animal>
            <AName>Growl</AName>
            <AAddress>Street</AAddress>
        </Animal>
        <Animal>
            <AName>Incognito</AName>
            <AAddress>Nowhere</AAddress>
        </Animal>
    </Animals>
    <People>
        <Person>
            <PName>Willy</PName>
            <PAddress>123 Common Lane</PAddress>
        </Person>
        <Person>
            <PName>Wonka</PName>
            <PAddress>Chocolate Factory</PAddress>
        </Person>
    </People>
</form>

我想从上述示例中生成 6 个唯一值(笛卡尔积),包含 的每个组合,其中它们都不为空.因此,此示例的输出将类似于(仅显示分组,需要所有子节点而不仅仅是名称):

I'd like to produce 6 unique values (the Cartesian Product) from the above sample, containing every combination of <Animal> and <Person> where neither of them are null. So, the output for this example would be like (just showing the grouping, need all child nodes instead of just names):

([Boomer, Willy], [Boomer, Wonka], [Growl, Willy], [Growl, Wonka], [Incognito, Willy], [Incognito, Wonka])

我正在尝试获取节点,而不仅仅是 元素值.因为我将整个 StyleVision 表单包装在这个模板中,所以范围应该在 级别.这样,在表单中,我可以只说 /AName,/PName, 并在每个副本上为 /AName/PName 获取一个值的形式.它应该充当元素节点的 6 个实例,每个实例代表 的唯一组合.

I'm trying to get the nodes, not just the <Name> element value. Because I'm wrapping the whole StyleVision form in this template, the scope should be at the <Animal> and <Person> level. This way, in the form I can just say /AName, /PName, and get one value for /AName and /PName on each copy of the form. It should act as 6 instances of an element node, each one representing a unique combination of both <Person> and <Animal>.

我查看了 for # in # return 语法,但我不知道如何返回唯一集.以下是我迄今为止尝试过的:

I looked into the for # in # return syntax but I cannot figure out how to return the unique set. Below is what I've tried so far:

for $a in form/Animals/Animal, $p in form/People/Person return ($a, $p)

form/Animals/Animal | form/People/Person

谁能指出我正确的方向?

Can anyone point me in the right direction?

谢谢!

推荐答案

更新

你的 for 表达式是正确的,你只需要添加一个元素构造函数就可以得到你需要的结果:

Your for expression is correct, you just need to add an element constructor to get the result you need:

for $animal in $x/Animals/Animal,
    $person in $x/People/Person
return element union { $animal, $person }

元素名称(示例中的union)可以是您想要的任何名称.

The element name (union in the example) can be anything that you want.

这些原始代码示例将从问题中生成结果字符串.

阅读起来有点困难,但这会满足您的需求:

It's a little hard to read, but this will do what you want:

concat("(",
  concat(string-join(
    for $animal in /form/Animals/Animal/Name,
        $person in /form/People/Person/Name
    return concat("[", concat(string-join(($animal, $person), ", "), "]")), ", "), ")"))

如果您可以使用 XPath/XQuery 3.0 处理器,则可以使用 || 连接运算符代替 concat 函数调用,这样(在某种程度上)更具可读性:

If you can use an XPath/XQuery 3.0 processor, you can use the || concatenation operator instead of the concat function calls, which is (somewhat) more readable:

"(" ||
  string-join(
    for $animal in /form/Animals/Animal/Name,
        $person in /form/People/Person/Name
    return "[" || string-join(($animal, $person), ", ") || "]"
    , ", ")
|| ")"

这篇关于如何使用 XPath 选择非空同级节点的笛卡尔积集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆