Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7982055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:33:45+00:00 2026-06-04T10:33:45+00:00

As a continuation in my thought patterns from this question: Saxon in Java: XSLT

  • 0

As a continuation in my thought patterns from this question: Saxon in Java: XSLT for CSV to XML

Per Michael Kay’s answer on that question, I eventually ended up with the following code for applying an XSLT to a document:

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();

This code makes use of the s9api in Saxon (I’m on version 9.4 HE). It allows me to set the initial template and dynamically inject the path to the document to be transformed, which allows me to transform non-XML files (such as CSV, in this particular case).

However, this somewhat obliterates my code-re-usability.

Let me explain: I have a transformDocument() method. Originally, before I was trying to do crazy things like transform CSV and was working only with XML, it was called by both my marshalObjectToDocument() and unmarshalDocumentToObject() methods (there’s the re-usability).

Here’s a comparison of the two directions given a world of only 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()).

In case 1 (unmarshalDocumentToObject()) I have a file path coming in, so I could just change transformDocument() to take a file path String and pass it that so it can manually inject it into the XSLT parameter. This would work for both XML and plain text.

In case 2 (marshalObjectToDocument()) I have NO file path. I have an object, which gets converted to a giant String containing its XML representation. I can’t pass a file path String to transformDocument() because I don’t have a file. Now I can’t use transformDocument(). Code re-usability destroyed.

My goal in all of this is both to be able to somehow treat both XML and plain text documents the same way in the code and to be able to re-use my code for applying XSLTs and XSDs whether I’m marshaling or unmarshaling. Is this a quixotic goal? Am I doomed to be relegated to writing different code for each document type and direction? Or can someone see a way around this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:33:47+00:00Added an answer on June 4, 2026 at 10:33 am

    Surely this is a standard and basic bit of software engineering? You’ve got three bits of code here: an application, which wants to run a transformation; and XSLT engine, which performs the transformation, and an interface layer, which provides an abstraction over the service offered by the XSLT engine, typically subsetting the functionality to provide only what the application needs, and doing it in a simpler form. The advantage of your interface layer is that it reduces the complexity of the transformation API; the disadvantage is that it also reduces the functionality. When your application starts wanting more of the functionality that was previously hidden, you have a number of choices: you can add functionality to the interface layer (eventually reaching the point where it ceases to add value), or you can bypass the interface layer for those parts of the application that get no value from it.

    Reuse of code depends on identifying globs of functionality that can be used in more than one place. If different bits of your application are doing different things then it becomes harder for them to reuse code. What’s new?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok - this is in continuation from my earlier question about sending an email
This is a continuation of this question: Original Question (SO) The answer to this
Ok this is a continuation from this question: How to make a simple Hello
This is a continuation from an existing question. Javascript - Goto URL based on
This is an in-depth continuation of my question from earlier this morning , which
This is a continuation question from Opencv video frame giving me an error I
This is a continuation of the question posted in: How to load a jar
This is a continuation of the post How does one access a method from
This is a continuation of the question here: JBoss - does app have to
This question is a continuation of my previous question here zend models architecture (big

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.