如何将XML输入从一种格式转换为另一种格式

时间:2019-02-06 23:33:00

标签: javascript xml xquery

我很难在2种XML输出格式之间进行转换。我有一个输入,其中包含一个学生及其进入服务的选择标记。输入看起来像这样:

<root>
    <searchCount>7</searchCount>
    <respData>
        <ferpaDeclaration>true</ferpaDeclaration>
        <nuId>1002</nuId>
        <hsn>false</hsn>
        <gni>false</gni>
        <privacy>false</privacy>
        <gdpr>true</gdpr>
        <adv>false</adv>
        <anb>false</anb>
        <shl>false</shl>
        <grd>false</grd>
        <doc>false</doc>
    </respData>
    <respData>
        <nuId>1001</nuId>
        <ferpaDeclaration>true</ferpaDeclaration>
        <hsn>false</hsn>
        <gni>true</gni>
        <privacy>false</privacy>
        <gdpr>true</gdpr>
        <adv>false</adv>
        <anb>true</anb>
        <shl>true</shl>
        <grd>false</grd>
        <doc>true</doc>
    </respData>
    <responseMessage>Success</responseMessage>
    <status>200</status>
</root>

我需要使用XQuery(在Informatica Cloud Real time中)将其转换为更通用的输出:

<student-optins>
  <student-category-optin>
    <nuid>1001</nuid>
    <category>arg</category>
    <optin>false</optin>
  </student-category-optin>
  <student-category-optin>
    <nuid>1001</nuid>
    <category>ferpaDeclaration</category>
    <optin>true</optin>
  </student-category-optin>
  <student-category-optin>
    <nuid>1001</nuid>
    <category>hsn</category>
    <optin>false</optin>
  </student-category-optin>
</student-optins>

2 个答案:

答案 0 :(得分:1)

您可以使用下面的XQuery脚本执行此操作。调整名称source.xml,将在末尾生成输出:

let $xml := for $x at $pos in doc("source.xml")/root/respData return ($x)
return
    <student-optins>
        { for $oi in $xml/*[not(self::nuId)] return 
            element {'student-category-optin'}
            {
                $oi/../nuId,
                element {'category'}
                {
                    local-name($oi)
                },
                element {'optin'}
                {
                    $oi/text()
                }
            }
        }
    </student-optins>

输出为:

<?xml version="1.0" encoding="UTF-8"?>
<student-optins>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>ferpaDeclaration</category>
        <optin>true</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>hsn</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>gni</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>privacy</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>gdpr</category>
        <optin>true</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>adv</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>anb</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>shl</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>grd</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1002</nuId>
        <category>doc</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>ferpaDeclaration</category>
        <optin>true</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>hsn</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>gni</category>
        <optin>true</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>privacy</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>gdpr</category>
        <optin>true</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>adv</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>anb</category>
        <optin>true</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>shl</category>
        <optin>true</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>grd</category>
        <optin>false</optin>
    </student-category-optin>
    <student-category-optin>
        <nuId>1001</nuId>
        <category>doc</category>
        <optin>true</optin>
    </student-category-optin>
</student-optins>

答案 1 :(得分:0)

假设输入已分配给$ myInput,则您的xQuery可能看起来像这样。

如果需要对输出进行排序,则可以执行orderby。

 {
   <student-optins>
    for $x in $myInput//respData
    return  
    <student-category-optin> 
    <nuid>{$x/nuid} </nuid> 
     <category>{$x/category}</category> 
    <optin>{$x/optin}</optin> 
    </student-category-optin>
}