Reputation: 9
I am trying to edit a XML file to put it into a graphic design program (indesign) and I want to use a value in the XML to get the attribute href with a XSLT file XML is:
<data>
<PetShop_1>
<Item>PetShop_1</Item>
<Cod_RMS>424919</Cod_RMS>
<Descricao_Produto>RACAO NHOCK CLASSIC 8KG</Descricao_Produto>
<Imagem>file:///G:/Drives%20compartilhados/Marketing/DESIGN/Cria%c3%a7%c3%b5es%202021/Tabloides/Boxes%20e%20tags/Pedro%20Jordan/vistas/6490226.jpg</Imagem>
</PetShop_1>
</data>
And I would like to get a transformed version, specially the image element to get the href attribute from a value of the original XML, and if possible, group the tags inside a father element
<data>
<PetShop_1>
<Sistema>
<Item>PetShop_1</Item>
<Cod_RMS>424919</Cod_RMS>
<Descricao_Produto>RACAO NHOCK CLASSIC 8KG</Descricao_Produto>
</Sistema>
<Imagem href="file:///G:/Drives%20compartilhados/Marketing/DESIGN/Cria%c3%a7%c3%b5es%202021/Tabloides/Boxes%20e%20tags/Pedro%20Jordan/vistas/6490226.jpg"></Imagem>
</PetShop_1>
</data>
Could someone help me?
Upvotes: 0
Views: 73
Reputation: 66723
The following XSLT achieves the desired results.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<!--default processing is to copy all attributes and nodes,
unless there are more specific matching templates to override that behavior -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PetShop_1">
<xsl:copy>
<!--add the Sistema element and group most content under it-->
<Sistema>
<!--do the normal copy for all elements except for Imagem -->
<xsl:apply-templates select="* except Imagem"/>
</Sistema>
<xsl:apply-templates select="Imagem"/>
</xsl:copy>
</xsl:template>
<!--change the Imagem text() into an attribute named href -->
<xsl:template match="Imagem/text()">
<xsl:attribute name="href" select="."/>
</xsl:template>
</xsl:stylesheet>
Upvotes: 3