2015-02-08 80 views
0

我想生成以下XML輸出:使用XQuery/XPath表達式生成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 

    ) 

如果我修改函數的局部:generateUpdate刪除的xmlns =「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>與命名空間「http://www.metasolv.com/OMS/OrderDataUpdate/2002/10/25

關聯任何幫助,將不勝感激。

在此先感謝。

回答

1

乍一看,它看起來好像通過聲明你的OrderDataUpdate元素上的默認命名空間,你已經引起了前綴的名字characteristicsname的解釋的變化,並在該元素詞法發生value。您的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  
}; 
+0

謝謝非常。這不可能比這更容易。將文本計算移出默認名稱空間聲明範圍的第二種方法適用於我。 – Avinash 2015-02-09 01:53:10

相關問題