User501
User501

Reputation: 347

Need to remove some elements in Json output using XSLT

I want to remove some element and made a interchange some element in the output of Json from the xml input using XSLT

My Input valid xml file is :

         <items>
            <item>
               <statement/>
               <response/>
               <media>
                  <type>img</type>
                  <link>SLP.jpg</link>
               </media>
            </item>
            <item>
               <statement>walking time bomb.</statement>
               <response>
                  <p>Developing additional problems with your health.</p>
               </response>
               <media>
                  <type>html</type>
                  <link>Brightcove.com%2FZ678766-1.avi</link>
               </media>
            </item>
</items>

XSL which i used as:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:json="http://json.org/" exclude-result-prefixes="#all">

    <xsl:template match="items">
            items: [
            <xsl:apply-templates/>
            ]
        </xsl:template>

        <xsl:template match="item">
            {
            <xsl:apply-templates/>
            },
        </xsl:template>

        <xsl:template match="statement">
            "statement": "<xsl:apply-templates/>"
        </xsl:template>

        <xsl:template match="response">
            "response": "<xsl:apply-templates/>"
        </xsl:template>

        <xsl:template match="media">
            media: {
            <xsl:apply-templates/>
            }
        </xsl:template>

        <xsl:template match="type">
            "type": "<xsl:apply-templates/>"
        </xsl:template>

        <xsl:template match="link">
            <xsl:text>"link": "files/</xsl:text>
            <xsl:value-of select="tokenize(., '/')[last()]"/>"
        </xsl:template>

        <xsl:template match="p">
            <xsl:apply-templates/>
        </xsl:template>

</xsl:stylesheet>

I get the output json as:

items: [

    {

    "statement": ""

    "response": ""

    media: {

    "type": "img"
    "link": "files/SLP.jpg"

    }

    },

    {

    "statement": "walking time bomb."

    "response": "Developing additional problems with your health."

    media: {

    "type": "html"
"link": "files/Brightcove.com%2FZ678766-1.avi"

    }

    },

But i need the output as like below:

media: {

        "type": "img"
        "link": "files/SLP.jpg"

        }

        items: [

        {

        "statement": "walking time bomb."

        "response": "Developing additional problems with your health."

        media: {

        "type": "html"
    "link": "files/Brightcove.com%2FZ678766-1.avi"

        }

        },

I need the first media file (jpg format) comes separately in the output as like above. Please help me out on this. Thanks in advance

Upvotes: 0

Views: 876

Answers (1)

Tim C
Tim C

Reputation: 70638

If you want the first media file to come separately, then you can select it separately by changing the current template matching items

<xsl:template match="items">
    <xsl:apply-templates select="item[1]/media" />
    items: [
    <xsl:apply-templates select="item[position() &gt; 1]"/>
    ]
</xsl:template>

All other templates can remain the same.

Upvotes: 2

Related Questions