Solved

java.net.MalformedURLException: no protocol for entity in XML

Posted on 2007-12-04
5
4,638 Views
Last Modified: 2013-11-23
Hello,
I have a Java program that transform XML files to another format using Saxon 8.7. It works fine for a single full XML file. When I try to transform a XML with entity file references, I got the no protocol error.
 
The XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//My Document DTD v1.1 20030520//EN" "mydtd/project.dtd" [
      <!ENTITY file_01 SYSTEM "file_01.xml">
      <!ENTITY file_02 SYSTEM "file_02.xml">
      <!ENTITY file_03 SYSTEM "file_03.xml">
      <!ENTITY file_04 SYSTEM "file_04.xml">
]>
<document xmlns:xlink="http://www.w3.org/1999/xlink">
      <title>Title here</title>
&file_01;
&file_02;
&file_03;
&file_04;
</document>

The error is:
net.sf.saxon.trans.DynamicError: java.net.MalformedURLException: no protocol: file_01.xml

I have catalog file to map my DTD so those are working fine. But as for the entity files, each project has different file so I don't think I map each of them.

Is there any solution in transforming this without getting the protocol error? Thank you for any help.

Owon. S.
0
Comment
Question by:owonseed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:R7AF
ID: 20411118
Can you explain a bit more what you're trying to do? Are you using XSLT to transform the XML into another format?

Is it correct that your code includes those file_xx.xml documents into one big XML file? How does that work and does it mean the declaration is included as well? Is the resulting XML well formed?
0
 

Author Comment

by:owonseed
ID: 20413392
Yes, I'm using XSLT (Saxon 8.7) to transform the XML to HTML format. Instead of one big XML file, my main XML contains only the external entity references to the smaller files (file_xx.xml). Main reason is the smaller files are created separately, so we only use external entity for those to keep thing simple.

Both of the main and smaller XML files are valid. When I put them together into one big file, it validates without any problem. In fact I can transformed it fine.

I can transform the main + external entity xml fine ** if ** I run it on local Windows desktop. The DTD is valid as well as a separate project.dtd file.

The problem only appears when it is using external entity, on the server. I have a java program that does the transform.

In my main xml, I can't use file:///file_xx.xml because I can't validate it properly using XML editor. In Windows it tries to find it in C:\file_xx.xml.  Nor can I use the full path to the file (file:///C:/myfolder/xslt/project_1/file_xx.xml. Because the path will not be valid after I put the file to the server. So relative path is the only way to go I believe. But this became a problem where the XSLT processor couldn't find the file_xx.xml because there is no protocol defined.

Using the external entity is the requirement so I can't put the content together as one big xml file.

O.S
0
 
LVL 13

Expert Comment

by:R7AF
ID: 20414170
You can use the document() function in XSLT to read multiple XML documents. This example assumes the file_xx.xml files are in the same folder as the xml and xslt. That is easiest to start with, but you can move them around like you do with HTML, using relative paths etc.

http://www.xml.com/pub/a/2002/03/06/xslt.html
<!-- index.xml -->
 
<?xml version="1.0" encoding="utf-8"?>
 
<document xmlns:xlink="http://www.w3.org/1999/xlink">
   <title>Title here</title>
   <files>
      <file>file_01.xml</file>
      <file>file_02.xml</file>
      <file>file_03.xml</file>
      <file>file_04.xml</file>
   </files>
</document>
 
<!-- file_01.xml -->
 
<?xml version="1.0" encoding="UTF-8"?>
<test>
	<id>345</id>
</test>
 
<!-- test.xsl -->
 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:template match="files">
		<xsl:apply-templates select="file"/>
	</xsl:template>
	
	<xsl:template match="file">
		<xsl:apply-templates select="document(.)/path/to/node"/>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 13

Accepted Solution

by:
R7AF earned 250 total points
ID: 20414182
Sorry, test.xsl should be:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:template match="files">
		<xsl:apply-templates select="file"/>
	</xsl:template>
	
	<xsl:template match="file">
		<xsl:apply-templates select="document(.)/test/id"/>
	</xsl:template>
</xsl:stylesheet>

Open in new window

0
 

Author Comment

by:owonseed
ID: 20459608
Thanks for the suggestion. Unfortunately the file format and the external entity references must remain unchanged. I'll try to implement your solution on my program but the files were created from a conversion process that I can't change. Part of the requirment is that I shouldn't change any of the source either. That's a bummer. Seems like I have no control over the file format. Therefore I need to find another way to deal with the external entity.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question