0

I have some XML that looks like this:

<a:feed>
     <a:id>http://localhost:8089/</a:id>
     <a:entry>
         <a:id>test1</a:id>
         <a:title/>
         <a:summary/>
         <a:updated>2016-07-05T13:34:37Z</a:updated>
         <a:author>
         <a:name/>
         </a:author>
         <a:link rel="edit" href="test1"/>
         <a:category>
     </a:entry>
     <a:entry>
         <a:id>test2</a:id>
         <a:title/>
         <a:summary/>
         <a:updated>2016-07-05T13:34:39Z</a:updated>
         <a:author>
         <a:name/>
         </a:author>
         <a:link rel="edit" href="test2"/>
         <a:category>
     </a:entry>
 </a:feed>

I want to load this XML in Scala and remove all text in the <a:updated> tags. How can I do this? Still new to Scala, can't figure this out.

1 Answer 1

0

As in your previous question, you can use a RewriteRule in conjunction with RuleTransformer for this. Given the particular form of your tags, you can directly pattern match on them.

val removeText = new RewriteRule {
  override def transform(n: Node): NodeSeq = n match  {
    case <a:updated>{_}</a:updated> => <a:updated></a:updated>
    case _ => n
  }
}

val output = new RuleTransformer(removeText).transform(xml)

However, note that your input XML is a bit malformed - the <a:category> tags are never closed. That corrected, I got the following interaction in scala:

scala> val xml= <a:feed>
     |              <a:id>http://localhost:8089/</a:id>
     |              <a:entry>
     |                  <a:id>test1</a:id>
     |                  <a:title/>
     |                  <a:summary/>
     |                  <a:updated>2016-07-05T13:34:37Z</a:updated>
     |                  <a:author>
     |                  <a:name/>
     |                  </a:author>
     |                  <a:link rel="edit" href="test1"/>
     |                  <a:category/>
     |              </a:entry>
     |              <a:entry>
     |                  <a:id>test2</a:id>
     |                  <a:title/>
     |                  <a:summary/>
     |                  <a:updated>2016-07-05T13:34:39Z</a:updated>
     |                  <a:author>
     |                  <a:name/>
     |                  </a:author>
     |                  <a:link rel="edit" href="test2"/>
     |                  <a:category/>
     |              </a:entry>
     |          </a:feed>
xml: scala.xml.Elem = ..

scala> val output = new RuleTransformer(removeText).transform(xml)
output: Seq[scala.xml.Node] =
         <a:feed>
             <a:id>http://localhost:8089/</a:id>
             <a:entry>
                 <a:id>test1</a:id>
                 <a:title/>
                 <a:summary/>
                 <a:updated></a:updated>
                 <a:author>
                 <a:name/>
                 </a:author>
                 <a:link rel="edit" href="test1"/>
                 <a:category/>
             </a:entry>
             <a:entry>
                 <a:id>test2</a:id>
                 <a:title/>
                 <a:summary/>
                 <a:updated></a:updated>
                 <a:author>
                 <a:name/>
                 </a:author>
                 <a:link rel="edit" href="test2"/>
                 <a:category/>
             </a:entry>
         </a:feed/>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.