Skip to content

Loop example request #2

@freewind

Description

@freewind

Could you give an example that can loop on each element, and do some transformation?

Say, an XML:

<div>
  <div class="header">This is header</div>
  <p class="body>This is body</p>
</div>

Is it possible to write a macro, which can add the id attribute to each tag? An also change the content of This is header.

The final xml element will be:

<div id="1">
  <div  id="2" class="header">This is header !!!!</div>
  <p  id="3" class="body>This is body</p>
</div>

The id value is not important, I just want to find a way to do something to each element, use pattern matching, like:

def transform(elem:Tree): Tree = {
    elem match {
       case q"<div>...</div>" => ... 
       case q"<p>...</p>" => ...
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions