2014-03-13 43 views
0

由于各种原因,我想从下面的scala elem对象中仅删除范围的某些部分。我意识到xmlns被定义了两次,这是我的程序失败的原因的一部分,但这是我必须从我打来的服务中使用。从scala xml对象中删除范围的某些部分

val dataXML = <DataSet xmlns="" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
       <Table BillCycleCode="20131218" Amount="135.6200" BillEndDate="2014-01-17T00:00:00-06:00" BillStartDate="2013-12-18T00:00:00-06:00" msdata:rowOrder="0" diffgr:id="Table1"/> 
       <Table BillCycleCode="20140118" Amount="126.5500" BillEndDate="2014-02-17T00:00:00-06:00" BillStartDate="2014-01-18T00:00:00-06:00" msdata:rowOrder="1" diffgr:id="Table2"/> 
       <Table BillCycleCode="20140218" Amount="126.5500" BillEndDate="2014-03-17T00:00:00-05:00" BillStartDate="2014-02-18T00:00:00-06:00" msdata:rowOrder="2" diffgr:id="Table3"/> 
       </DataSet> 

dataXML.scope揭示了以下内容:

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns="" 

基本上我希望能够做一些类型的过滤器,这样,如果(particularNamespace =的xmlns:diffgr =“瓮:架构 - 微软-com:xml-diffgram-v1“或者specialNamespace!= xmlns:msdata =”urn:schemas-microsoft-com:xml-msdata“)remove specialNamespace。

我的最终结果应该是这个样子:

<DataSet xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> 
    <Table BillCycleCode="20131218" Amount="135.6200" BillEndDate="2014-01-17T00:00:00-06:00" BillStartDate="2013-12-18T00:00:00-06:00" msdata:rowOrder="0" diffgr:id="Table1"/> 
    <Table BillCycleCode="20140118" Amount="126.5500" BillEndDate="2014-02-17T00:00:00-06:00" BillStartDate="2014-01-18T00:00:00-06:00" msdata:rowOrder="1" diffgr:id="Table2"/> 
    <Table BillCycleCode="20140218" Amount="126.5500" BillEndDate="2014-03-17T00:00:00-05:00" BillStartDate="2014-02-18T00:00:00-06:00" msdata:rowOrder="2" diffgr:id="Table3"/> 
</DataSet> 

我是相当新,在斯卡拉使用XML,所以任何帮助将不胜感激!感谢大家!

回答

0

你需要应该看起来有点像这样(我没有测试,但只写了类似的东西)代码:

def yourFilterFunction(prefix : String) : Boolean = .... 

def scopeFilter(scope : NamespaceBinding) : NamespaceBinding = 
    if (scope == TopScope) TopScope else 
    if (yourFilterFunction(scope.prefix)) 
     new NamespaceBinding(scope.prefix, scope.uri, scopeFilter(scope.parent)) 
    else 
     scopeFilter(scope.parent) 

def removeScopes(node : Node) : Node = node match { 
    case Elem(prefix, label, attr, scope, children @ _*) => 
    Elem(prefix, label, attr, scopeFilter(scope), children.map(removeScopes(_)):_*) 
    case _ => node 
} 

您需要实现过滤功能,对于每一个未使用的前缀返回false。然后你可以在元素节点上调用removeScopes。

祝你好运