thanks excellent answer provided tim c in how transform xml based on attributes json , ignore attributes especific elements using xslt/xsl, learn how transform input xml json , excluding item attributes during transformation. kindly, see input xml file below.
please, how can exact same transformation ignoring "aa" , "bbb" in output json? mean, how exclude during xml transformation json record identifiers named aa , bbb in case?
in real world, aa 2 numeric digits (e.g. 99) standing type of items , bbb 3 numeric digits (e.g. 999) standing other type of items. each product sum of aa plus bbb type of product. since number of digits tell me if aa type or bbb type, understand can have json otuput file valid both json , business perspectives getting outcome without "aa" , "bbb" words.
input file:
<c:product xmlns:c="myapp"> <c:item cod="789"> <c:aa name="024" value="123"/> <c:bbb name="0105" value="123456"/> <c:bbb name="0122" value="t"/> <c:aa name="071" value="00000001"/> </c:item> <c:item package="123" cod="11111"> <c:aa name="002" value="753"/> <c:aa name="003" value="456"/> <c:bbb name="0146" value="147852"/> </c:item> </c:product>
my current output file when current xsl applied:
[ { "aa" : {"024":"123"}, "bbb" : {"0105":"123456"}, "bbb" : {"0122":"t"}, "aa" : {"071":"00000001"} }, { "aa" : {"002":"753"}, "aa" : {"003":"456"}, "bbb" : {"0146":"147852"} } ]
my current xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0" xmlns:c="myapp" exclude-result-prefixes="c"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="*[c:item]"> <xsl:text>[ </xsl:text> <xsl:apply-templates select="c:item"/> <xsl:text>] </xsl:text> </xsl:template> <xsl:template match="c:item"> <xsl:text> { </xsl:text> <xsl:apply-templates /> <xsl:text> }</xsl:text> <xsl:if test="following-sibling::c:item"> <xsl:text>,</xsl:text> </xsl:if> <xsl:text> </xsl:text> </xsl:template> <xsl:template match="c:item/*"> <xsl:text> "</xsl:text> <xsl:value-of select="local-name()" /> <xsl:text>" : {"</xsl:text> <xsl:value-of select="@name" /> <xsl:text>":"</xsl:text><xsl:value-of select="@value" /> <xsl:text>"}</xsl:text> <xsl:if test="following-sibling::*"> <xsl:text>,</xsl:text> </xsl:if> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet>
my desired output understand both business , json pattern valid :
[ { {"024":"123"}, {"0105":"123456"}, {"0122":"t"}, {"071":"00000001"} }, { {"002":"753"}, {"003":"456"}, {"0146":"147852"} } ]
the expected output give not valid json. perhaps want this
[ { "024":"123", "0105":"123456", "0122":"t", "071":"00000001" }, { "002":"753", "003":"456", "0146":"147852" } ]
with target, straight-forward tweak xslt produce te desired results. change change template matches c:item/*
remove output of local-name()
(which outputs name of elements; aaa
, bb
, etc) , surrounding braces.
try xslt:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0" xmlns:c="myapp" exclude-result-prefixes="c"> <xsl:output method="text"/> <xsl:strip-space elements="*"/> <xsl:template match="*[c:item]"> <xsl:text>[ </xsl:text> <xsl:apply-templates select="c:item"/> <xsl:text>] </xsl:text> </xsl:template> <xsl:template match="c:item"> <xsl:text> { </xsl:text> <xsl:apply-templates /> <xsl:text> }</xsl:text> <xsl:if test="following-sibling::c:item"> <xsl:text>,</xsl:text> </xsl:if> <xsl:text> </xsl:text> </xsl:template> <xsl:template match="c:item/*"> <xsl:text> "</xsl:text> <xsl:value-of select="@name" /> <xsl:text>":"</xsl:text><xsl:value-of select="@value" /> <xsl:text>"</xsl:text> <xsl:if test="following-sibling::*"> <xsl:text>,</xsl:text> </xsl:if> <xsl:text> </xsl:text> </xsl:template> </xsl:stylesheet>
do take time study answer, understand each part doing. using http://xsltransform.net/ modify xslt can see outcome of each change is.
Comments
Post a Comment