0

我有一个在我的 Xquery 请求中定义的表单。此表单建立在 XSD 架构上。

我的表格:

<xsd:element name="AddInsuranceToOrderForm">
        <xsd:complexType>
              <xsd:sequence>
                    <xsd:element name="OrderId" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                    <xsd:element name="DistributionChannel" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                    <xsd:element name="FloatNumber" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                    <xsd:element name="UserId" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                    <xsd:element name="SalesId" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                    <xsd:element name="TotalDue" type="xsd:decimal" minOccurs="1" maxOccurs="1"/>
                    <xsd:element name="ChangeDue" type="xsd:decimal" minOccurs="1" maxOccurs="1"/>
                    **<xsd:element ref="tns:tenderItem" minOccurs="0" maxOccurs="unbounded"/>**
                    <xsd:element name="ProtectionPlans" type="tns:ProtectionPlanItemType" minOccurs="0" maxOccurs="unbounded"/>
                    <xsd:element name="InsurancePlans" type="tns:FeatureItemType" minOccurs="0" maxOccurs="unbounded"/>
              </xsd:sequence>
        </xsd:complexType>
  </xsd:element>

注意tenderItems 是一个参考。它的来源在这里:

<xsd:element abstract="true" name="tenderItem" type="tns:TenderType"/>
  <xsd:complexType name="TenderType">
        <xsd:sequence>
              <xsd:element maxOccurs="1" minOccurs="0" name="date" type="xsd:dateTime"/>
              <xsd:element maxOccurs="1" minOccurs="0" name="lineNumber" type="xsd:string"/>
              <xsd:element maxOccurs="1" minOccurs="0" name="tenderId" type="xsd:long"/>
              <xsd:element maxOccurs="1" minOccurs="0" name="returnTenderId" type="xsd:long"/>
              <xsd:element maxOccurs="1" minOccurs="0" name="tenderType" type="tns:TenderTypeType"/>
              <xsd:element maxOccurs="1" minOccurs="0" name="amount" type="xsd:decimal"/>
              <xsd:element maxOccurs="1" minOccurs="0" name="signature" type="tns:SignatureType"/>
        </xsd:sequence>
  </xsd:complexType>

有几个扩展tenderType 的特定投标,我想将它们添加到我的Xquery 请求中。这是一个例子:

      <xsd:element name="billToAccount" substitutionGroup="tns:tenderItem" type="tns:BillToAccountType"/>
  <xsd:complexType name="BillToAccountType">
        <xsd:complexContent>
              <xsd:extension base="tns:TenderType">
                    <xsd:sequence>
                          <xsd:element maxOccurs="1" minOccurs="0" name="accountId" type="xsd:long"/>
                          <xsd:element maxOccurs="1" minOccurs="0" name="subscriptionId" type="xsd:string"/>
                          <xsd:element maxOccurs="1" minOccurs="0" name="chargeDescription" type="xsd:string"/>
                          <xsd:element maxOccurs="unbounded" minOccurs="0" name="lineItems" type="tns:BillToAccountLineItemType"/>
                    </xsd:sequence>
              </xsd:extension>
        </xsd:complexContent>
  </xsd:complexType>

那么这在 Xquery 中是如何完成的呢?我尝试了以下方法:

{
    for $BillToAccount in $addInsuranceToOrderABM1/ns3:MessagePayload/ns3:AddInsuranceToOrder/ns1:AddInsuranceToOrderDataArea/ns2:TenderItem
        return
            <ns2:billToAccount>
                {
                    for $Date in $BillToAccount/ns0:Date
                    return
                        <ns2:date>{ data($Date) }</ns2:date>
                }…
}

我为每个tenderType 都这样做了,但是映射器不支持XML 元素。

4

1 回答 1

1

您可以使用schema-element(ns3:tenderItem)匹配该替换组中的任何元素。

import schema default element namespace "http://www.example.com/xml-namespace/" at "schema.xsd";

declare variable $input as schema-element(AddInsuranceToOrderForm)
   := validate { doc('data.xml')/AddInsuranceToOrderForm };

for $item in $input/schema-element(tenderItem)
return $item

schema.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns:tns="http://www.example.com/xml-namespace/"
    targetNamespace="http://www.example.com/xml-namespace/">
    <xsd:element name="AddInsuranceToOrderForm">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="OrderId" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                <xsd:element name="DistributionChannel" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                <xsd:element name="FloatNumber" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                <xsd:element name="UserId" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                <xsd:element name="SalesId" type="xsd:long" minOccurs="1" maxOccurs="1"/>
                <xsd:element name="TotalDue" type="xsd:decimal" minOccurs="1" maxOccurs="1"/>
                <xsd:element name="ChangeDue" type="xsd:decimal" minOccurs="1" maxOccurs="1"/>
                <xsd:element ref="tns:tenderItem" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element abstract="true" name="tenderItem" type="tns:TenderType"/>
    <xsd:complexType name="TenderType">
        <xsd:sequence>
            <xsd:element maxOccurs="1" minOccurs="0" name="date" type="xsd:dateTime"/>
            <xsd:element maxOccurs="1" minOccurs="0" name="lineNumber" type="xsd:string"/>
            <xsd:element maxOccurs="1" minOccurs="0" name="tenderId" type="xsd:long"/>
            <xsd:element maxOccurs="1" minOccurs="0" name="returnTenderId" type="xsd:long"/>
            <xsd:element maxOccurs="1" minOccurs="0" name="amount" type="xsd:decimal"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="BillToAccount" substitutionGroup="tns:tenderItem" type="tns:BillToAccountType"/>
    <xsd:complexType name="BillToAccountType">
        <xsd:complexContent>
            <xsd:extension base="tns:TenderType">
                <xsd:sequence>
                    <xsd:element maxOccurs="1" minOccurs="0" name="accountId" type="xsd:long"/>
                    <xsd:element maxOccurs="1" minOccurs="0" name="subscriptionId" type="xsd:string"/>
                    <xsd:element maxOccurs="1" minOccurs="0" name="chargeDescription" type="xsd:string"/>
                </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
</xsd:schema>

并且data.xml作为:

<?xml version="1.0" encoding="UTF-8"?>
<AddInsuranceToOrderForm xmlns="http://www.example.com/xml-namespace/">
    <OrderId>1</OrderId>
    <DistributionChannel>1</DistributionChannel>
    <FloatNumber>1</FloatNumber>
    <UserId>1</UserId>
    <SalesId>1</SalesId>
    <TotalDue>1</TotalDue>
    <ChangeDue>1</ChangeDue>
    <BillToAccount>
        <date>2012-10-01T23:59:59</date>
        <lineNumber>line 1</lineNumber>
        <tenderId>1</tenderId>
        <returnTenderId>1</returnTenderId>
        <amount>1.2</amount>
        <accountId>1</accountId>
        <subscriptionId>1</subscriptionId>
        <chargeDescription>1</chargeDescription>
    </BillToAccount>
</AddInsuranceToOrderForm>

给出输出:

<?xml version="1.0" encoding="UTF-8"?>
<BillToAccount xmlns="http://www.example.com/xml-namespace/">
    <date>2012-10-01T23:59:59</date>
    <lineNumber>line 1</lineNumber>
    <tenderId>1</tenderId>
    <returnTenderId>1</returnTenderId>
    <amount>1.2</amount>
    <accountId>1</accountId>
    <subscriptionId>1</subscriptionId>
    <chargeDescription>1</chargeDescription>
</BillToAccount>
于 2012-10-01T20:31:52.640 回答