Convert XML elements to XML attributes using XSLT -
i working on project , team mate generated xml file not compatible code , want convert xml file using xslt. want convert xml
<frame> <frameno>0</frameno> <objectlist> <object> <confidence>0.95</confidence> <box> <h>775</h> <w>202</w> <xc>509</xc> <yc>8.6</yc> </box> </object> <object> <confidence></confidence> <box> <h>966</h> <w>220</w> <xc>1779</xc> <yc>1080</yc> </box> </object> </objectlist> </frame>
to xml file
<frame number="0"> <objectlist> <object confidence = "0.95"> <box h="775" w="202" xc="509" yc="8.68"/> </object> <object confidence = "0.50"> <box h="996" w="220" xc="1779" yc="1080" /> </object> </objectlist> </frame>
anyone please me convert xml using xslt.
though forum not meant questions of kind, willing give solution since in hurry.
another important lesson need learn xml acts api between , teammate. should have discussed before, prevent inconsistencies have now.
the xslt need is:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="frame"> <xsl:element name="frame"> <xsl:attribute name="number"> <xsl:value-of select="frameno" /> </xsl:attribute> <xsl:apply-templates select="objectlist" /> </xsl:element> </xsl:template> <xsl:template match="objectlist"> <xsl:element name="objectlist"> <xsl:apply-templates select="./object" /> </xsl:element> </xsl:template> <xsl:template match="object"> <xsl:element name="object"> <xsl:attribute name="confidence"> <xsl:if test="normalize-space(confidence) != '' "> <xsl:value-of select="confidence" /> </xsl:if> <xsl:if test="normalize-space(confidence) = '' "> <xsl:text>0.50</xsl:text> </xsl:if> </xsl:attribute> <xsl:apply-templates select="./*[not(self::confidence)]" /> </xsl:element> </xsl:template> <xsl:template match="box"> <xsl:element name="box"> <xsl:for-each select="*"> <xsl:attribute name="{name()}" > <xsl:value-of select="text()" /> </xsl:attribute> </xsl:for-each> </xsl:element> </xsl:template> </xsl:stylesheet>
i hope take time study understand it.
Comments
Post a Comment