簡體   English   中英

如何將XML輸入從一種格式轉換為另一種格式

[英]How do I convert the XML input from one format to another

我在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>

您可以使用下面的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>

假設輸入已分配給$ 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>
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM