Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

style sheets

Posted on 2012-03-29
8
Medium Priority
?
500 Views
Last Modified: 2012-08-14
Can style sheets be used to filter xml data to basically hide certain fields?
Example:
If I wanted to filter the following xml below to only have <fielda>?
<test>
  <fielda>abc</fielda>
  <fieldb>123</fieldb>
</test>
0
Comment
Question by:dpalyca755
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 752 total points
ID: 37785313
You can use an XSLT stylesheet to transform one XML into another XML.
And yes you can programm the stylesheet in a way that it keeps all the elements intact but filter out fieldb... or any other logic you would want.

XSLT stylesheets area very powerfull mechanism to manipulate XML trees
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1248 total points
ID: 37785316
I think this gives several examples of how to remove some of the nodes from XML:
http://stackoverflow.com/questions/3717215/remove-xml-node-using-java-parser
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1248 total points
ID: 37785321
this gives the code how to remove the element:

from

http://www.kodejava.org/examples/479.html

package org.kodejava.example.jdom;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;

import java.io.File;
import java.io.IOException;

public class JDOMRemoveElement {
    public static void main(String[] args) {
        SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(new File("userinfo.xml"));

            //
            // The lines below output the original userinfo.xml content
            //
            // <?xml version="1.0" encoding="UTF-8"?>
            // <rows>
            //   <row>
            //     <firstname>Alice</firstname>
            //     <lastname>Mallory</lastname>
            //     <address>Sunset Road</address>
            //   </row>
            // </rows>
            //            
            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
            out.output(doc, System.out);

            //
            // Remove the address element from Alice information. First we
            // get the row element from the root element, and finally 
            // remove the address from the row. And the result will be:
            //
            // <?xml version="1.0" encoding="UTF-8"?>
            // <rows>
            //   <row>
            //     <firstname>Alice</firstname>
            //     <lastname>Mallory</lastname>
            //   </row>
            // </rows>
            //
            doc.getRootElement().getChild("row").removeChild("address");
            out.output(doc, System.out);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window

0
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 752 total points
ID: 37785333
Given that the question was "can stylesheets do this?" ...
here is an example of a stylesheet with two templates
one for general copy and continue processing (identity transform)
and one template that removes the fieldb

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="fieldb"/>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 752 total points
ID: 37785341
And here is one that removes all but the fielda element

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/">
           <xsl:copy-of select="//fielda"/>
    </xsl:template>

</xsl:stylesheet>

Open in new window


Note that you can parameterize that so that you can pass in the name of the element you want to filter out

generally simple tasks like this could be done in java code directly. XML processing rapidly reaches a complexity that is best done in XSLT
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1248 total points
ID: 37785347
I tested this code:

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;


import java.io.File;
import java.io.IOException;

public class JDOMRemoveElement {
    public static void main(String[] args) {
        SAXBuilder builder = new SAXBuilder();
        try {
            org.jdom.Document doc = builder.build(new File("yourXML.xml"));


            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
       //     out.output(doc, System.out);

            
            doc.getRootElement().getChild("test").removeChild("fieldb");
            out.output(doc, System.out);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window



Input:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<test>
  <fielda>abc</fielda>
  <fieldb>123</fieldb>
</test>
</root>

Open in new window


Output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <test>
    <fielda>abc</fielda>
  </test>
</root>

Open in new window

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1248 total points
ID: 37785356
This code takes input exactly as you posted it and removes filedb

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;


import java.io.File;
import java.io.IOException;

public class JDOMRemoveElement {
    public static void main(String[] args) {
        SAXBuilder builder = new SAXBuilder();
        try {
            org.jdom.Document doc = builder.build(new File("yourXML.xml"));


            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
           // out.output(doc, System.out);

            
            doc.getRootElement().removeChild("fieldb");
            out.output(doc, System.out);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window

Input:
<test>
  <fielda>abc</fielda>
  <fieldb>123</fieldb>
</test>

Open in new window


Output:

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <fielda>abc</fielda>
</test>

Open in new window

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1248 total points
ID: 37785388
and this code does the same thing as above but writes to the output file:

You need to download
http://www.jdom.org/dist/binary/jdom-1.1.3.zip

and then extract jar flile jdom-1.1.3.jar
and put it in the classpath


import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class JDOMRemoveElement {
    public static void main(String[] args) {
        SAXBuilder builder = new SAXBuilder();
        try {
            org.jdom.Document doc = builder.build(new File("yourXML.xml"));


            XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
         //   out.output(doc, System.out);

            
            doc.getRootElement().removeChild("fieldb");
            out.output(doc, new FileOutputStream("yourXML1.xml"));
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Open in new window


Input file:

<test>
  <fielda>abc</fielda>
  <fieldb>123</fieldb>
</test>

Open in new window


Output file:

<?xml version="1.0" encoding="UTF-8"?>
<test>
  <fielda>abc</fielda>
</test>

Open in new window

0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question