I have an xml file that i want to add an element = carrier to and give it a value = ABF. the original file looks like this
<?xml version="1.0"?>
<ABF>
<QUOTEID>L9V5442611</QUOTEID>
<CHARGE>166.08</CHARGE>
<ADVERTISEDTRANSIT>1 Day</ADVERTISEDTRANSIT>
i am using this xslt file below to make the following changes
1- add QUOTETENDER as the beginning and ending elements .. 2- remove ABF 3- insert a CARRIER element and give it the value of ABF
i was able to achieve 1 and 2 but can not get 3 done
the result i need is the xml snippet at the bottom
... can someone help ..thanks
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="no"/>
<xsl:template match="ABF">
<xsl:element name="QUOTETENDER">
<xsl:element name="CARRIER"></xsl:element>
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<QUOTETENDER>
<CARRIER>ABF</CARRIER>
<QUOTEID>L9V5442611</QUOTEID>
<CHARGE>166.08</CHARGE>
<ADVERTISEDTRANSIT>1 Day</ADVERTISEDTRANSIT>
</QUOTETENDER>
CodePudding user response:
You just need to add ABF
inside of the CARRIER element:
<xsl:element name="CARRIER">ABF</xsl:element>
You could simplify things and use literal element declarations, since your element names are static and not dynamically determined:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="no"/>
<xsl:template match="ABF">
<QUOTETENDER>
<CARRIER>ABF</CARRIER>
<xsl:apply-templates select="@*|node()"/>
</QUOTETENDER>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
You could also use the local-name()
of the matched ABF
element:
<CARRIER><xsl:value-of select="local-name()"/></CARRIER>