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

时间:2015-02-08 22:54:14

标签: xquery

我正在尝试生成以下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 =&#34; http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25" ;,那么TetheringType的文本内容将被填充生成的输出xml,如下所示:

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

我做错了什么?有人可以向我解释为什么在向OrderDataUpdate元素添加xmlns =“http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25”时,xml节点的文本内容不会填充在xml输出中。我需要<OrderDataUpdate>与命名空间相关联&#34; http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25&#34;

任何帮助都将不胜感激。

提前致谢。

1 个答案:

答案 0 :(得分:1)

乍一看,看起来好像通过在OrderDataUpdate元素上声明默认命名空间,您对未加前缀的名称characteristicsname和{{1}的解释发生了变化在该元素中以词汇形式出现的。您的value元素没有文本内容,因为XPath没有匹配(输入中的命名空间TetheringType中没有characteristics元素。)

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

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

http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25

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

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     
};