I have the below json file
sample.json
{
"books": [
{
"test": {
"count": 1
},
"Name": "C",
"Type": "String"
},
{
"test": {
"count": 2
},
"Name": "C++",
"Type": "String"
}
]
}
I have to extract the "Name" field value in xslt. If I am removing the test block, it's working fine. but with test block I am getting empty result.
I have tried with below xslt sample.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://www.example.com/mf"
exclude-result-prefixes="#all"
expand-text="yes">
<xsl:output method="json" indent="yes"/>
<xsl:function name="mf:apply-templates" as="item()*">
<xsl:param name="items" as="item()*"/>
<xsl:apply-templates select="$items"/>
</xsl:function>
<xsl:template match="." name="xsl:initial-template">
<xsl:sequence select="array { mf:apply-templates(?books?*) }"/>
</xsl:template>
<xsl:template match=".[. instance of map(xs:string, xs:anyAtomicType)]">
<xsl:map>
<xsl:map-entry key="'resourceType'" select="'Test'"/>
<xsl:map-entry key="'identifier'">
<xsl:map>
<xsl:map-entry key="'name'"
select="?Name" />
</xsl:map>
</xsl:map-entry>
</xsl:map>
</xsl:template>
</xsl:stylesheet>
Desired Output should be as below:
output.json
[
{
"resourceType": "Test",
"identifier": { "name":"C" }
},
{
"resourceType": "Test",
"identifier": { "name":"C++" }
}
]
You can use
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all">
<xsl:output method="json" indent="yes"/>
<xsl:template match="." name="xsl:initial-template">
<xsl:sequence
select="array {
?books?* ! map {
'resourceType' : 'Test',
'indentifier' : map { 'name' : ?Name }
}
}"/>
</xsl:template>
</xsl:stylesheet>