Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop example request #2

Open
freewind opened this issue Mar 7, 2015 · 2 comments
Open

Loop example request #2

freewind opened this issue Mar 7, 2015 · 2 comments

Comments

@freewind
Copy link

freewind commented Mar 7, 2015

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>" => ...
    }
}
@freewind
Copy link
Author

freewind commented Mar 7, 2015

Just realised since we can get an instance of Elem, so is the way to iterate it (or transform it) the same as what we normally do? Like https://gist.github.com/zentrope/728034?

@densh
Copy link

densh commented Mar 12, 2015

Yes, it's the same. Elem is regular xml node and behaves like one. The only difference is that it also contains special Unquote parts which corresponds to Scala expressions within XML.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants