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

标签 javascript xml xquery

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

相关文章:

javascript - 在元素外部单击不起作用

javascript - 为什么我收到此错误 "undefined is not a function "

c# - 如何在 C# 中向从数据集 Writexml 生成的 XML 添加额外信息?

xml - 带有谓词和条件的XPath表达式

Xquery:计算一组记录中每个记录中某个术语的出现次数

xquery - 如何获取 xdmp :unquote($record) 的基本 URI

javascript - 排序在javascript中有连字符的数组

javascript - 在 chrome.storage 中保存数组

c# - 如何将具有不同名称但具有相同属性集的 xml 元素反序列化为类型化数组/集合

java - 如何针对字符串中的 XML 运行 XQuery?