gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:14:49 24 4
gpt4 key购买 nike

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

关于javascript - 如何将 XML 输入从一种格式转换为另一种格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54564189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com