JAXP - Fusing XSLT transformation results into a single XML file

B

Blue Gecko

Hi all

I should fuse into an XML file the results dynamically generated by XSLT
transformations from some source files (attachment [2]).
The issue is that, obviously, each XSLT transformation generates its own
processing instructons (PIs, for example <?xml ...?>), so I end up (as
you can see in attachment [1]) with a sintactically-broken XML file.
Could you suggest me the most elegant way to resolve such a challenge?

Many thanks

[1] Resulting XML file:
<?xml version='1.0' encoding='iso-8859-1'?>
<siteMap>
<node href="index.page.teg">
<!-- This is the insertion point of the XSLT result from the
index.page.teg file -->
<?xml version="1.0" encoding="iso-8859-1"?>
<info/>
<node href="about/index.page.teg">
<!-- This is the insertion point of the XSLT result from the
about/index.page.teg file -->
<?xml version="1.0" encoding="iso-8859-1"?>
<info/>
</node>
</node>
</siteMap>

[2] Code recursively generating the node hierarchy:
/*
Note:
PrintWriter siteMapWriter is the writing stream of the resulting XML
file (see [1]).
*/
private void buildNodeList(
NodeList siteNodeList
)
{
for(
int index = 0;
index < siteNodeList.getLength();
index++
)
{
Node siteNode = siteNodeList.item(index);
String pageHref =
siteNode.getAttributes().getNamedItem("href").getNodeValue();

// Print the map node start!
siteMapWriter.print("<node href=\"" + pageHref + "\">");

File pageFile = new File(basePath + pageHref);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);

try
{
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(pageFile);
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(this.siteMapWriter);
pageInfoExtractor.transform(source, result);
}
//<snip content="catches"/>

// Print the map subnodes!
buildNodeList(
((Element)siteNode).getElementsByTagName("node")
);

// Print the map node end!
siteMapWriter.print("</node>");
}
}
 
B

Blue Gecko

Blue said:
Hi all

I should fuse into an XML file the results dynamically generated by XSLT
transformations from some source files (attachment [2]).
The issue is that, obviously, each XSLT transformation generates its own
processing instructons (PIs, for example <?xml ...?>), so I end up (as
you can see in attachment [1]) with a sintactically-broken XML file.
Could you suggest me the most elegant way to resolve such a challenge?

Found the trick: a smart guy suggested me to use the
omit-xml-declaration attribute of xsl:eek:utput element from the XSLT spec.
[1] Resulting XML file:
<?xml version='1.0' encoding='iso-8859-1'?>
<siteMap>
<node href="index.page.teg">
<!-- This is the insertion point of the XSLT result from the
index.page.teg file -->
<?xml version="1.0" encoding="iso-8859-1"?>
<info/>
<node href="about/index.page.teg">
<!-- This is the insertion point of the XSLT result from the
about/index.page.teg file -->
<?xml version="1.0" encoding="iso-8859-1"?>
<info/>
</node>
</node>
</siteMap>

[2] Code recursively generating the node hierarchy:
/*
Note:
PrintWriter siteMapWriter is the writing stream of the resulting XML
file (see [1]).
*/
private void buildNodeList(
NodeList siteNodeList
)
{
for(
int index = 0;
index < siteNodeList.getLength();
index++
)
{
Node siteNode = siteNodeList.item(index);
String pageHref =
siteNode.getAttributes().getNamedItem("href").getNodeValue();

// Print the map node start!
siteMapWriter.print("<node href=\"" + pageHref + "\">");

File pageFile = new File(basePath + pageHref);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);

try
{
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(pageFile);
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(this.siteMapWriter);
pageInfoExtractor.transform(source, result);
}
//<snip content="catches"/>

// Print the map subnodes!
buildNodeList(
((Element)siteNode).getElementsByTagName("node")
);

// Print the map node end!
siteMapWriter.print("</node>");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top