json - Unable to fetch attributes from XML using XSLT -
i trying extract values of attributes xml json using xslt.
xml
<root1> <root2> <test attr1="123" attr2="abc"/> </root2> </root1>
json
{ "attr1":"<xsl:value-of select="root1/root2/test/[@attr1"]/>" "attr2":"<xsl:value-of select="root1/root2/test/[@attr2"]/>" }
i tried many solutions none of them working. headers , footer xslt taken care.
when writing xsl should adopt best practices , use templates repeatative content. in example should use template transform attributes so..
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="2.0"> <xsl:template match="/"> <xsl:text>{</xsl:text> <xsl:apply-templates select="root1/root2/test/@*"/> <xsl:text>}</xsl:text> </xsl:template> <xsl:template match="@*"> <xsl:if test="position()>1"><xsl:text>,</xsl:text></xsl:if> <xsl:text>"</xsl:text> <xsl:value-of select="name()"/> <xsl:text>":"</xsl:text> <xsl:value-of select="."/> <xsl:text>"</xsl:text> </xsl:template> </xsl:stylesheet>
in xsl above i'm using template single attribute json name/value pair , apply-templates used select attributes root/root2.test node.
Comments
Post a Comment