Remove elements from XML. using XSLT and JAXP [modified]
-
I am trying to remove some unwanted elements from a source XML document using XSLT and JAXP and create a new XML document in which those unwanted elements are not present. My XSLT works fine and removes the desired elements when there is no XMLNS attribute defined in root node of XML document. But the same java code & XSLT doesn't removes those elements when XMLNS attribute is set in root node of source XML document. In following example, I am trying to remove "outstation" elements from XML doc. Sample XML: abc21@yahoo.com xyz21@yahoo.co.in abc21@hotmail.com xyz21@msn.com 12345678 12345670 08012345678 08012345670 Sample XSLT: Java code snippet: public static void main(String[] args) throws Exception { .... .... Source xmlSource = new StreamSource(srcFile); Source xsltSource = new StreamSource(xsltFile); StreamResult result = new StreamResult(resFile); TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(xsltSource); trans.transform(xmlSource, result); ... ... } Is there anyway to solve this problem, other than using DOM? -- modified at 8:59 Wednesday 18th July, 2007
-
I am trying to remove some unwanted elements from a source XML document using XSLT and JAXP and create a new XML document in which those unwanted elements are not present. My XSLT works fine and removes the desired elements when there is no XMLNS attribute defined in root node of XML document. But the same java code & XSLT doesn't removes those elements when XMLNS attribute is set in root node of source XML document. In following example, I am trying to remove "outstation" elements from XML doc. Sample XML: abc21@yahoo.com xyz21@yahoo.co.in abc21@hotmail.com xyz21@msn.com 12345678 12345670 08012345678 08012345670 Sample XSLT: Java code snippet: public static void main(String[] args) throws Exception { .... .... Source xmlSource = new StreamSource(srcFile); Source xsltSource = new StreamSource(xsltFile); StreamResult result = new StreamResult(resFile); TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(xsltSource); trans.transform(xmlSource, result); ... ... } Is there anyway to solve this problem, other than using DOM? -- modified at 8:59 Wednesday 18th July, 2007
Well strangely I am reply to my posted query .. In the given example, it is expected that template in the XSLT, , should remove all occurrence of "outstation" elements from the XML document. This XSLT will work fine for any XML document which doesn't has a "DEFAULT NAMESPACE" declared in root element. In given example, I have declared a default namespace in root element as 'xmlns="http://www.contacts.com/transform"'. Because of the default namespace, the expanded name of element is pair of URI name and Element name (URI name here is "http://www.contacts.com/transform"). So the template defined in XSLT for "outstation" element will fail to match any element in XML file. As the namespaces link the XML & XSLT through a URI. So the solution to this problem is to add a namespace prefix with the namespace name "http://www.contacts.com/transform" in the xslt file and then use the prefix in the XPath statements as shown below: Solution is in XSLT: ... ... ...