Selector de atributo
Ejercicio
Sección titulada «Ejercicio»Dado el siguiente payload de entrada:
<?xml version="1.0"?><catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> <book id="bk103"> <author>Corets, Eva</author> <title>Maeve Ascendant</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-11-17</publish_date> <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description> </book></catalog>Escribí una transformación DataWeave que produzca la siguiente salida:
<?xml version='1.0' encoding='UTF-8'?><book> <id>bk102</id> <title>Midnight Rain</title> <author>Ralls, Kim</author></book>Opcional:
- Utilizar una variable para almacenar el libro deseado y luego utilizarlo en el Script.
- Presta atención que en este caso la salida es un XML, por ende siempre debe haber una clave/etiqueta que englobe todo el objeto.
- En este ejercicio se espera obtener el segundo libro, pero las etiquetas
bookse encuentran repetidas. Recordá que podés utilizar el selector múltiple.*y el selector de índice[<n>]para obtener el valor esperado. - Para obtener un valor de atributo que se encuentra dentro de una etiqueta, se puede utilizar el selector de atributo
.@.
Ver solución
%dw 2.0output application/xml
var book: Object = payload.catalog.*book[1]---book: { id: book.@id, title: book.title, author: book.author}
/* Sin variable:book: { id: payload.catalog.*book[1].@id, title: payload.catalog.*book[1].title, author: payload.catalog.*book[1].author} */{Code mat;}
Aprendé MuleSoft: cursos, ejercicios y exámenes. Gratis y sin registro.
© 2026 Codebymat