modify attribute of root element

33 views
Skip to first unread message

Marko D

unread,
Feb 5, 2013, 12:38:21 PM2/5/13
to scale...@googlegroups.com
I'm trying to modify an attribute of the root element in an XML like:

<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
    <wb-module deploy-name="test">
  </project-modules>

The value of attribute "id" should be modified from "moduleCoreId" to "123".
So far I tried (in a Scala worksheet):

 val testXml = loadXml(new FileReader("org.eclipse.wst.common.component"))
                                                  //> testXml  : scales.xml.Doc = scales.xml.doc
  
  val root = top(testXml).*("project-modules"l)   //> root  : scales.xml.XPath[List[scales.utils.Path[scales.xml.XmlItem,scales.xm
                                                  //| l.Elem,[T]scales.utils.ImmutableArrayProxy[T]]]] = scales.xml.XPath@2a8ceeea
                                                  //| 
  
  val idAtribPath = top(testXml).*("project-modules"l)\@("id"l)
                                                  //> idAtribPath  : scales.xml.AttributePaths[List[scales.utils.Path[scales.xml.X
                                                  //| mlItem,scales.xml.Elem,[T]scales.utils.ImmutableArrayProxy[T]]]] = Attribute
                                                  //| Paths(List(AttributePath(Attribute({}id,moduleCoreId),Path(Top,0,scales.xml.
                                                  //| Elem$NoNamespacesElem@e92d9317))),XPathInfo(List(List(Path(Top,0,scales.xml.
                                                  //| Elem$NoNamespacesElem@e92d9317))),false,false,false,true,false),scala.collec
                                                  //| tion.generic.GenTraversableFactory$ReusableCBF@1b31303f)
  
  string(idAtribPath)                             //> res0: String = moduleCoreId
  
  
  val folded=  foldPositions( root){
  p => {
 Replace(elem(p)/@("id" ->"123"))
      //AsIs()
       }
  }                                               //> folded  : Paths.this.FoldR[scales.xml.XmlItem,scales.xml.Elem,scales.xml.XCC
                                                  //| ] = Right(AddedBeforeOrAfterRoot)
          
 printTree(folded.left.get.tree) 

How can I replace the value of "id" attribute ?

Mark

Chris Twiner

unread,
Feb 5, 2013, 3:14:53 PM2/5/13
to scale...@googlegroups.com
Hi Mark,

I'm treating this as a bug, it made sense for the earliest
foldPosition implementations but doesn't now when using a Replace
(addbefore / after would still be incorrect for a root).

https://github.com/chris-twiner/scalesXml/issues/23

For changing it in this case its just:

root.tree /@( "id" -> "123" )

I'm targeting 0.5-M1 for that bug fix, which should be end of next week.

Cheers,
Chris
> --
> You received this message because you are subscribed to the Google Groups
> "scales-xml" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scales-xml+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Marko D

unread,
Feb 5, 2013, 3:26:11 PM2/5/13
to scale...@googlegroups.com
Thanks,
Nice syntax
Reply all
Reply to author
Forward
0 new messages