Solved

style sheets

Posted on 2012-03-29
8
491 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 188 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 312 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 312 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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 188 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 188 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 312 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 312 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 312 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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, …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now