I have found multiple tutorials, articles, posts etc. regarding xsl:for-each but none that seem to match my use case so apologies if this has already been answered elsewhere.
I have the following example XML (There are multiple doc items):
<doc>
<att>
<PRef>52545125</PRef>
<BlobContent Name="rtf" ID="A7A9348763A111EA887800505685156C">
<Property Name="MimeType" Value="application/rtf"/>
<Property Name="FileName" Value="P0001A__m.rtf"/>
<Reference Link="Directory\export_819280402327681192_P0001A__m.rtf"
Type="RELATIVEFILE"/>
</BlobContent>
<InvoiceDate>01/01/1970</InvoiceDate>
<NetAmount>100.75</NetAmount>
<GrossAmount/>
<BlobContent Name="tif" ID="A7A90D7663A111EA887800505685156C">
<Property Name="MimeType" Value="image/tiff"/>
<Property Name="FileName" Value="P0001A__m.tif"/>
<Reference Link="Directory\export_4747308861722121077_P0001A__m.tif"
Type="RELATIVEFILE"/>
</BlobContent>
</att>
I'm attempting to change the following from each BlobContent
<Reference Link="Directory\export_4747308861722121077_P0001A__m.tif" Type="RELATIVEFILE"/>
to read as:
<rtf>Directory\export_819280402327681192_P0001A__m.rtf</rtf>
<tif>Directory\export_4747308861722121077_P0001A__m.tif</tif>
The following xsl will work as expected but puts the data into tags with nothing to define is this is the tif or rtf:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/doc/att/BlobContent/Reference">
<xsl:variable name="element-name" select="@Link"/>
<xsl:element name="Link">
<xsl:value-of select="@Link"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I've tried adding a for-each to create the new tags based on the BlobContent name but get an error stating {tif rtf} is an invalid QName so it seems to be picking up each value but grouping them into a single element name.
Thanks
CodePudding user response:
Use a template
<xsl:template match="/doc/att/BlobContent/Reference">
<xsl:element name="{../@Name}">
<xsl:value-of select="@Link"/>
</xsl:element>
</xsl:template>