How to read and output XML processing instructions in Scala?

I am writing a small Scala application that does the following:

1) Reading XML / XHTML files

2) Do a little preprocessing

3) If necessary, change it using the XSLT stylesheet.

4) Process it a little.

5) Save it as XHTML.

My XML files will start with something like:

<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="../xslt/default.xslt"?> 

If I read them with

 scala.xml.XML.load(scala.xml.Source.fromFile(file)) 

I get Elem, but I'm losing XML processing instructions. I resorted to reading it as a String, doing String manipulations to find the xml stylesheet, and then passed it to

 scala.xml.XML.load(scala.xml.Source.fromString(text)) 

There must be a better way to do this. I need to know which stylesheet I should use inside Scala , because Scala should call the XSLT processor if necessary.

Also, after I finished processing them, I save them with

 scala.xml.Utility.trim(transformed).buildString(true) 

but the resulting document does not contain XML declarations as well as HTML DOCTYPE. I also want to have them.

I know that these are technically two questions, but these are basically both ends of the same problem, and I suspect that the solution to the second problem is related to the solution to the first.

+4
source share
1 answer

Basically, Scala XML is inadequate for your needs. Even with the xml.parsing.XhtmlParser that the Document generates, you only get the version, encoding, and dtd. You can do an analysis parser, an event parser, or override XML using a special SAXParser to get XSLT material, but you still cannot submit this information using Scala XML, and you still have to pass your save code to add this material back .

Therefore, I suggest you use one of the Java libraries that handles XSLT.

+2
source

All Articles