2

I'm reading an existing XML file and outputting it (using DOM).

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test"?>
<Books>
    <Book name="MyBook" />
</Books>

But how do I modify the XML stylesheet? -> href here set "test".

2 Answers 2

4

Something like this should work (untested)

Element root = doc.getDocumentElement();
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/processing-instruction('xml-stylesheet')";
ProcessingInstruction pi;
pi = (ProcessingInstruction)xpath.evaluate(expression, doc, XPathConstants.NODE);
pi.setData("type='text/xsl' href='foo.xsl'");
Sign up to request clarification or add additional context in comments.

Comments

0

Thats a bit tricky, but why not read the file first into a String and do a replace before sending it via a stream into the dom parser.

3 Comments

OK can be closed got a solution :)
i parsed and found the xml-stylesheet and simply replaced it with my own attributes
xmlhandler: Questions aren't "closed", you can just Approve the answer that helped you (by clicking the checkbox beside the answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.