English 中文(简体)
撒克逊在爪哇:XSLT 代码再使用
原标题:Saxon in Java: XSLT Code Reuse

作为我在这个问题上的思维模式的延续:

Michael Kay在回答这个问题时, 最终为文件应用XSLT的代码如下:

Processor processor = new Processor(false);
StringWriter stringWriter = new StringWriter();
Serializer serializer = new Serializer(stringWriter);
XsltCompiler compiler = processor.newXsltCompiler();
XsltExecutable executable = compiler.compile(new StreamSource(new File(transformLocation)));
XsltTransformer transformer = executable.load();
transformer.setInitialTemplate(new QName("main"));
transformer.setParameter(new QName("filePath"), new XdmAtomicValue("location/of/Test.csv"));
transformer.setDestination(serializer);
transformer.transform();
String transformedDocument = stringWriter.toString().trim();

本代码使用撒克逊语中的 s9api (Im on version 9.4 HE) 。 它允许我设置初始模板并动态输入要转换的文档路径, 从而允许我转换非 XML 文件( 如 CSV, 具体情况下为 CSV )。

然而,这在某种程度上抹去了我的代码可恢复性。

让我解释一下: 我有一种 transforDocument () 方法。 最初, 在我尝试变换 CSV 和只用 XML 来工作之前, 它被我的 < code> marshalObjectToDocument () 和 unmarshalDocumentToobject () 方法调用( 这里有可重新使用的方法 ) 。

在此比较两个方向, 给出一个只有 XML 的世界 :

  1. unmarshalDocumentToObject()
    • I start with a file that has the document inside of it.
    • I do this: new StreamSource(new File(documentLocation))
    • This StreamSource can be passed to transformDocument as the "source" (XsltTransformer.setSource()).
  2. marshalObjectToDocument()
    • I start with on object of some sort.
    • This gets marshaled into a giant String of the XML.
    • I do this: new StreamSource(new StringReader(giantStringOfXML))
    • This StreamSource can be passed to transformDocument as the "source" (XsltTransformer.setSource()).

在情况1 (unmarshalDocumentToObject () ) 中,我有一个文件路径正在进来, 这样我就可以更改 transforDocument () 来选择文件路径字符串并通过它, 这样它就可以手动将其输入 XSLT 参数。 这将对 XML 和 普通文本都有效 。

在案件2( marshalObjectToDocument () ) I have no file path. 我有一个对象, 它被转换成包含 XML 代表的巨型字符串 。 我无法通过文件字符串路径到 transforDocument () , 因为我没有文件 。 现在我无法使用 transforDocument () . 代码已销毁 。

我的目标是既能以某种方式在代码中以同样的方式对待 XML 和 普通文本文档,又能重新使用我的代码来应用 XSLT 和 XSD 。 这是否是一个不划一的目标? 我注定要为每个文件类型和方向写不同的代码吗? 或者有人能看到一条路吗?

最佳回答

这肯定是软件工程的一个标准和基本部分。 您在这里得到了三位代码 : 应用程序, 它想要运行一个转换; 和 XSLT 引擎, 它可以进行转换, 和一个界面层, 它为 XSLT 引擎提供的服务提供了抽象的抽象化, 通常将功能进行分解, 仅提供应用程序需要的功能, 并以更简单的形式进行。 您的界面层的优点在于它会降低转换 API 的复杂性; 其缺点在于它也会降低功能。 当您的应用程序开始想要更多以前隐藏的功能时, 您有几种选择: 您可以将功能添加到界面层( 最终达到它停止增加价值的点), 或者您可以绕过应用程序中那些没有价值的部分的界面层 。

代码的重新使用取决于能否识别多个地方可以使用的功能的球球。 如果您的应用程序有不同部分在做不同的事情, 那么他们就更难重新使用代码。 新的是什么?

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签