简体   繁体   English

从XPATH创建XML

[英]Creating XML from XPATH

I am a new bee with xml and XSL, work with legacy platforms... 我是使用xml和XSL的新手,可以使用旧版平台...
I am looking for a solution to create XML from XPATH. 我正在寻找从XPATH创建XML的解决方案。 Happen to see this post How to Generate an XML File from a set of XPath Expressions? 碰巧看到这篇文章如何从一组XPath表达式生成XML文件? 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. 与“注释”部分中讨论的请求类似,我试图将整个XSLT作为字符串传递,并使用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=" document('') " />. 但是当将XSL作为字符串传递时,它抱怨“ document()”,它是<xsl:variable name =“ vStylesheet” select =“ document('') ” />的一部分。
Error is "SXXP0003: Error reported by XML parser: Content is not allowed in prolog." 错误为“ SXXP0003:XML解析器报告错误:序言中不允许内容。” 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. 我的基本要求是我应该能够将XSL(整个文件或“ vPop”部分)作为字符串传递,并且应该在不涉及任何文件的情况下在另一个字符串中接收结果。 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... 这样,我可以提高性能并使它通用,这样我们商店中任何不知道如何处理XML的人仍然可以生成一个XML。

My java code looks like.. 我的Java代码看起来像..

 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. 我传递的XSLT字符串与之前的文章相同。

When you call document(''), it treats '' as a relative URI reference to be resolved against the base URI of the stylesheet. 当您调用document('')时,它将''作为要相对于样式表的基本URI解析的相对URI引用。 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. 如果样式表的基本URI未知,则此方法将不起作用,当您将其作为StreamSource提供(包装不带SystemId的StringReader时)时,情况就是如此。

In the case of Saxon, document('') actually needs to re-read the stylesheet. 对于Saxon,document('')实际上需要重新读取样式表。 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. 因此,您需要(a)提供URI作为StreamSource上的systemId属性(任何URI都会提供,实际上不会被读取),以及(b)提供URIResolver来解决对document('' )并提供原始字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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