I hate writing XSLT, mainly because I’m just no good at it. I have an XML data file with a very bad content model, where everything is an attribute instead of a direct value inside a meaningful node.
I’d like to run an XSLT Transform that could turn this:
<token-value-entry token-name="RECIPIENT_CITY">
<token-value><value>ROSEBURG</value></token-value>
</token-value-entry>
<token-value-entry token-name="RECIPIENT_STATE">
<token-value><value>OR</value></token-value>
</token-value-entry>
Into this:
<RECIPIENT_CITY>ROSEBURG</<RECIPIENT_CITY>
<RECIPIENT_STATE>OR</RECIPIENT_STATE>
Can any generous soul give me some direction?
Phil
April 30, 2019, 11:47pm
2
Something like this should work, assuming the input data file is valid XML:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="token-value-entry">
<xsl:element name="{@token-name}">
<xsl:value-of select="./token-value/value" />
</xsl:element>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Thanks, my solution ended up being more complex, but I did essentially begin with something very close to this!
My solution had to be more complex because the xml itself was more complex than what I posted. But it did blow the dust off of my XSLT.
Thanks!
Phil,
I appreciate your participation in this forum, just wanted to say that publicly.
TDG
Phil
May 2, 2019, 7:06pm
5
If I weren’t past a certain age when blushing becomes impossible, I would…