2015-02-08 54 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

相关问题