简体   繁体   中英

Creating XML from XPATH

I am a new bee with xml and XSL, work with legacy platforms...
I am looking for a solution to create XML from XPATH. Happen to see this post How to Generate an XML File from a set of XPath Expressions? which helped me a lot.
Similar to the request discussed under "Comments" section, I am trying to pass whole XSLT as string, and receiving result as a sting back using Saxon. Receiving result as string, no issues. But when passing XSL as string, it complain about "document()" which is part of < xsl:variable name="vStylesheet" select=" " />. ” />的一部分。
Error is "SXXP0003: Error reported by XML parser: Content is not allowed in prolog." My basic requirement is I should be able to pass XSL (whole file or "vPop" portion) as a string and should receive result in another string without involving any files. That way I can improve the performance and make it generic so that anyone in our shop who does not know how to deal with XML can still generate one...

My java code looks like..

 public static String simpleTransform(final String xsltStr) {

     String strResult = "";

     TransformerFactory tFactory = TransformerFactory.newInstance();
     String tempXMLStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<OpnXMLTAG>Dummy</OpnXMLTAG>";

     try {
         StringWriter writer = new StringWriter();
         StreamResult result = new StreamResult(writer);
         StreamSource XSLSource = new StreamSource(new StringReader(xsltStr));

         Transformer transformer = tFactory.newTransformer(XSLSource );

         transformer.transform(new StreamSource(new StringReader(tempXMLStr)), result);

         strResult = writer.toString();

     } catch (Exception e) {
         e.printStackTrace();
     }

     return strResult;

 }

And XSLT string I am passing is the same from earlier post.

When you call document(''), it treats '' as a relative URI reference to be resolved against the base URI of the stylesheet. This won't work if the base URI of the stylesheet is unknown, which is the case when you supply it as a StreamSource wrapping a StringReader with no systemId.

In the case of Saxon, document('') actually needs to re-read the stylesheet. It doesn't keep the source file around at run-time just in case it's needed. So you'll need to (a) supply a URI as the systemId property on the StreamSource (any URI will do, it won't actually be read), and (b) supply a URIResolver to resolve the call on document('') and supply the original string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM