Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

style sheets

Posted on 2012-03-29
8
Medium Priority
?
503 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

783 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