XQuery合并元素和子元素

时间:2015-09-30 01:38:05

标签: xml xml-parsing xquery

我正在处理大量的XML文件,这些文件在它们之间重复出现了共同的元素。我已经能够将它们连接成一个文件并使用Xquery进行排序,但是我很难在下一步基于密钥标识符合并元素。例如,我有一个XML文件,其结构如下:

<example>
    <Store ID="111">
        <Manager ID="123">
            <Employee>
                <EmployeeID>0001001</EmployeeID>
                <HireDate Value="1-Jan-2000"/>
                <Action ID="001" Type="S">
                    <Details ID="a1">
                        <TransactionType>I</TransactionType>
                    </Details>
                    <TransactionType>R</TransactionType>
                </Action>
                <TransactionType>R</TransactionType>
            </Employee>
            <TransactionType>R</TransactionType>
        </Manager>
        <TransactionType>R</TransactionType>
    </Store>
    <Store ID="111">
        <Manager ID="123">
            <Employee>
                <EmployeeID>0001001</EmployeeID>
                <HireDate Value="1-Jan-2000"/>
                <Action ID="003" Name="Ecg" Type="S">
                    <Details ID="b1">
                        <TransactionType>I</TransactionType>
                    </Details>
                    <TransactionType>R</TransactionType>
                </Action>
                <TransactionType>R</TransactionType>
            </Employee>
            <TransactionType>R</TransactionType>
        </Manager>
        <TransactionType>R</TransactionType>
    </Store>
    <Store ID="00102">
        <Manager ID="00302">
            <Employee>
                <EmployeeID>0002001</EmployeeID>
                <Sex Value="M"/>
                <Confidential Birthdate="1970-07-03"/>
                <Action ID="003" Name="Ecg" Type="S">
                    <Details ID="c1">
                        <TransactionType>I</TransactionType>
                    </Details>
                    <TransactionType>R</TransactionType>
                </Action>
                <TransactionType>R</TransactionType>
            </Employee>
            <TransactionType>R</TransactionType>
        </Manager>
        <TransactionType>R</TransactionType>
    </Store>
</example>

我希望能够根据Store ID,Manager ID和EmployeeID的元素值的属性值合并前两个主要Store元素,以便生成的XML如下所示:

<example>
    <Store ID="111">
        <Manager ID="123">
            <Employee>
                <EmployeeID>0001001</EmployeeID>
                <HireDate Value="1-Jan-2000"/>
                <Action ID="001" Type="S">
                    <Details ID="a1">
                        <TransactionType>I</TransactionType>
                    </Details>
                    <TransactionType>R</TransactionType>
                </Action>
                <Action ID="003" Name="Ecg" Type="S">
                    <Details ID="b1">
                        <TransactionType>I</TransactionType>
                    </Details>
                    <TransactionType>R</TransactionType>
                </Action>
                <TransactionType>R</TransactionType>
            </Employee>
            <TransactionType>R</TransactionType>
        </Manager>
        <TransactionType>R</TransactionType>
    </Store>
    <Store ID="00102">
        <Manager ID="00302">
            <Employee>
                <EmployeeID>0002001</EmployeeID>
                <Sex Value="M"/>
                <Confidential Birthdate="1970-07-03"/>
                <Action ID="003" Name="Ecg" Type="S">
                    <Details ID="c1">
                        <TransactionType>I</TransactionType>
                    </Details>
                    <TransactionType>R</TransactionType>
                </Action>
                <TransactionType>R</TransactionType>
            </Employee>
            <TransactionType>R</TransactionType>
        </Manager>
        <TransactionType>R</TransactionType>
    </Store>
</example>

任何建议:Xquery实现此结果的方法将受到高度赞赏 - 或任何其他方法(例如XSLT?)。谢谢!

1 个答案:

答案 0 :(得分:1)

XQuery 1.0缺乏任何分组功能,这使得这很棘手。如果您可以访问XQuery 3.0,则可以使用新的&#34; group by&#34;构造

同样在XSLT中,1.0中没有内置的分组功能,但是在2.0中。使用XSLT 2.0,您通常会这样做:

Item item = new Item ("item", 1658);
String s = "<span style='font-family: 'Comic Sans MS';>TOTAL HT &nbsp; = totalItem </span>";
StandardEvaluationContext itemContext = new StandardEvaluationContext(item);
Expression exp = parser.parseExpression(s);
s2 = exp.getValue(itemContext, String.class);

我在这里做了一些假设:你只想要第一个HireDate,但是你想要所有的动作。您必须根据实际需求进行调整。