xml - An item of type 'Attribute' cannot be constructed within a node of type 'Root -
i'm trying figure how add attribute root node. have following xslt transform 2 different types of xml files. 1st xml file transformed fine have problem when second xml file xslt throws error "an item of type 'attribute' cannot constructed within node of type 'root" how fix in xslt
xslt file
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:output method="xml" indent="yes"/> <!--check whether lossformsversion exists if not write--> <xsl:template match="inspection[not(@lossformsversion)]"> <xsl:attribute name="lossformsversion">07-25-2013-1-54</xsl:attribute> </xsl:template> <!--replace lossformsversion templates version--> <xsl:template match="inspection/@lossformsversion"> <xsl:attribute name="lossformsversion">07-25-2013-1-54</xsl:attribute> </xsl:template> <!--copy rest of document is--> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
1st xml file (before transformation)
<?xml version="1.0" encoding="utf-8" ?> <inspection lossformsversion="07-25-2013-1-52"> . . . </inspection>
1st xml file (after transformation)
<?xml version="1.0" encoding="utf-8" ?> <inspection lossformsversion="07-25-2013-1-54"> . . . </inspection>
2nd xml file (before transformation)
<?xml version="1.0" encoding="utf-8" ?> <inspection> . . . </inspection>
2nd xml file after transformation should 1st transformed xml file. in advance
<xsl:template match="inspection[not(@lossformsversion)]"> <xsl:copy> <xsl:attribute name="lossformsversion">07-25-2013-1-54</xsl:attribute> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
for 2nd xml, template matches element write attribute output. xsl:copy copies Ïnspection node , attribute writes that.
Comments
Post a Comment