生成 XML 输出且 XQuery / XPath 表达式不匹配,为什么?

Generating XML output with XQuery / XPath expression matching nothing, why?

我正在尝试生成以下 xml 输出:

<OrderDataUpdate xmlns=”http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25”>
<TetheringType>IE Tethering</TetheringType>
</OrderDataUpdate>

但是我得到了这个(请注意未填充 TetheringType 的文本内容)。

<OrderDataUpdate xmlns="http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25">
<TetheringType/>
</OrderDataUpdate>

我的输入XML:

<soapenv:Envelope 
     xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:serviceLookUpResponse 
           xmlns:ser="http://xmlns.oracle.com/communications/inventory/webservice/service">
         <com:requestContext xsi:nil="true" 
            xmlns:com="http://xmlns.oracle.com/communications/inventory/webservice/common"/>
         <com:messages xmlns:com="http://xmlns.oracle.com/communications/inventory/webservice/common"
         >Success</com:messages>
         <service>
            <id>12021409</id>
            <name>1</name>
            <description xsi:nil="true"/>
            <state>in service</state>
            <startDate>2013-11-06T00:13:02.000-06:00</startDate>
            <specification>
               <name>HSPA Wireless Service</name>
               <entityType>Service</entityType>
               <description xsi:nil="true"/>
               <startDate>2010-05-13T00:00:01.000-05:00</startDate>
               <endDate xsi:nil="true"/>
            </specification>
            <parties>
               <id>1-3BQJ7K</id>
               <name>MTSC NETWORK SRV WIRELESS-CELLULAR</name>
               <description xsi:nil="true"/>
            </parties>
            <configurations>
               <version>31</version>
               <previousVersion>30</previousVersion>
               <id>Se_12021409_31</id>
               <name>Se_12021409_31</name>
               <description xsi:nil="true"/>
               <state>completed</state>
               <startDate>2015-01-21T15:03:52.000-06:00</startDate>
               <endDate xsi:nil="true"/>
               <configurationItems>
                  <name>Entitlement</name>
                  <index>0</index>
                  <resourceAssignment>
                     <state>assigned</state>
                     <resource>
                        <id>Tethering</id>
                        <name xsi:nil="true"/>
                        <description>Tethering Entitlement</description>
                        <specification>
                           <name>Entitlement</name>
                           <entityType>Custom Network Address</entityType>
                           <description xsi:nil="true"/>
                           <startDate>2015-01-15T00:00:01.000-06:00</startDate>
                           <endDate xsi:nil="true"/>
                        </specification>
                     </resource>
                  </resourceAssignment>
                  <resourceReference xsi:nil="true"/>
                  <characteristics>
                     <name>Tethering Type</name>
                     <value>IE Tethering</value>
                  </characteristics>
               </configurationItems>               
            </configurations>
         </service>
      </ser:serviceLookUpResponse>
   </soapenv:Body>
</soapenv:Envelope>

Xquery:

declare namespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
declare namespace ser = "http://xmlns.oracle.com/communications/inventory/webservice/service";
declare namespace oms = "urn:com:metasolv:oms:xmlapi:1";

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "xml";
declare option output:indent "yes";

declare function local:generateUpdate($uimResponse as element()*) as element()
{

    let $update := 
            <OrderDataUpdate xmlns="http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25">
                    <TetheringType>{ $uimResponse//characteristics[name='Tethering Type']/value/text() }</TetheringType>
            </OrderDataUpdate>
    return 
        $update     
};



let $uimResponse := fn:root(.)/soap:Envelope/soap:Body/ser:serviceLookUpResponse
let $mdn := $uimResponse//configurationItems[name = 'MDN']/resourceAssignment/resource/name/text()    
let $output := local:generateUpdate($uimResponse)   
return
    (
        $output

    )

如果我修改函数 local:generateUpdate 以删除 xmlns="http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25",那么 TetheringType 的文本内容将填充到生成的输出 xml 中,看起来像下面:

<OrderDataUpdate>
<TetheringType>IE Tethering</TetheringType>
</Add>
</OrderDataUpdate>

我做错了什么?有人可以向我解释为什么添加 xmlns=”http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25” to the OrderDataUpdate element, the text content of xml nodes are not populated in the xml output. I need the <OrderDataUpdate> to be associated with the namespace "http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25"

如有任何帮助,我们将不胜感激。

提前致谢。

乍一看,似乎通过在您的 OrderDataUpdate 元素上声明默认名称空间,您已经导致对无前缀名称 characteristicsname 和 [= 的解释发生了变化14=] 在该元素中按词法出现。您的 TetheringType 元素没有文本内容,因为 XPath 没有匹配任何内容(您输入的名称空间 http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25 中没有 characteristics 元素)。

乍一看,XPath 规范和我检查过的 XQuery 处理器的行为证实了这一假设。

至少有两种简单的方法可以解决您的问题。首先,您可以通过不声明默认名称空间来避免名称捕获:

declare function local:generateUpdate(  
  $uimResponse as element()*
) as element() {
  let $update := <odu:OrderDataUpdate 
                    xmlns:odu=
                    "http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25">
                   <odu:TetheringType>{ 
                     $uimResponse//characteristics[name='Tethering Type']
                     /value/text() 
                   }</odu:TetheringType>
                 </odu:OrderDataUpdate>
  return $update     
};

或者您可以将文本计算移出默认命名空间声明的范围:

declare function local:generateUpdate(
  $uimResponse as element()*
) as element() {
  let $tttext := $uimResponse
                     //characteristics[name='Tethering Type']
                     /value/text() 
  let $update := <OrderDataUpdate 
                   xmlns="http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25">
                    <TetheringType>{ 
                     $tttext
                   }</TetheringType>
                 </OrderDataUpdate>
  return $update     
};