Solved

XML to PDF, attributes with spaces

Posted on 2010-11-11
19
373 Views
Last Modified: 2012-05-10
Hello.

We recieve several XML-documents that we need to convert to PDF. I have downloaded the apache-FOP and tried converting the documents, but I'm having some trouble with some of the attribute names.

This is a simplified version of the XML-document:
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<test test="testing" anothervariable="testing2" \>
</config>

This is a simplified version of the XSL-document:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>PC</h2>
  <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

<xsl:template match="config">
  <p>
  <xsl:apply-templates select="test test"/>
  <xsl:apply-templates select="anotervariable"/>
  </p>
</xsl:template>

<xsl:template match="test test">
  Title: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

<xsl:template match="anothervariable">
  Artist: <span style="color:#00ff00">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

</xsl:stylesheet>

Open in new window


The command I'm running to convert the file is:
fop -xml xmldocument.xml -xsl xsldocument.xsl -pdf pdfdocument.pdf

I have never worked with xml or xsl, so I have no idea where to begin. But it seems the problem occurs when a variable name contains a space. If the variable is named test_test, it would work. If I name the variable test test, it fails.
0
Comment
Question by:nebb-tgr
  • 9
  • 8
  • 2
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Yes, spaces won't work
0
 
LVL 1

Author Comment

by:nebb-tgr
Comment Utility
Could you try to explain in a little more detail? I know that it can be done, are you saying it can't be done by using xsl? Could you suggest another way of converting a xml-document to pdf that would be useful?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to use names without spaces
0
 
LVL 1

Author Comment

by:nebb-tgr
Comment Utility
I understand that, but that is not possible at this point. I need to be able to use the XML-documents as they are.
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
The element name is not "test test" which is not allowed in XML
the element name is "test", it has an attribute test

replace this

<xsl:template match="config">
  <p>
  <xsl:apply-templates select="test test"/>
  <xsl:apply-templates select="anotervariable"/>
  </p>
</xsl:template>

<xsl:template match="test test">
  Title: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>


with this

<xsl:template match="config">
  <p>
  <xsl:apply-templates select="test/@test"/>
  <xsl:apply-templates select="anotervariable"/>
  </p>
</xsl:template>

<xsl:template match="test/@test">
  Title: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
The whole stylesheet corrected (but not tested)
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">



<xsl:template match="/">

  <html>

  <body>

  <h2>PC</h2>

  <xsl:apply-templates/>

  </body>

  </html>

</xsl:template>



<xsl:template match="config">

  <p>

  <xsl:apply-templates select="test/@test"/>

  <xsl:apply-templates select="test/@anotervariable"/>

  </p>

</xsl:template>



<xsl:template match="test/@test">

  Title: <span style="color:#ff0000">

  <xsl:value-of select="."/></span>

  <br />

</xsl:template>



<xsl:template match="test/@anothervariable">

  Artist: <span style="color:#00ff00">

  <xsl:value-of select="."/></span>

  <br />

</xsl:template>



</xsl:stylesheet>

Open in new window

0
 
LVL 1

Author Comment

by:nebb-tgr
Comment Utility
That does not work for me. What other possibilites are there for converting XML to PDF other that through XSL?
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
Honestly, there are a zillion ways to get to PDF.
But XSLT is one of the most convenient ways in my opinion.
You started of by stating that the space in the variable name (sic) was the problem.
I have corrected that.
Now you claim this doesn't work. I can tell you that it works.
BUT your stylesheet does not create a formatting object XSL.
So let me explain the process in detail.

FOP creates a PDF from a FO-XSL
XSLT creates an FO-XSL from XML
So this is a two step process.

We have only discussed the first step so far.
But you need more to get this to work.
Please let me know how you want the PDF to look like
and please give me a more extensive XML source example
0
 
LVL 1

Author Comment

by:nebb-tgr
Comment Utility
Thank you.

Coming up is part of the actual XML-document that I want to transform. It is built the same way as it will in the future.

My goal now is to be able to get this information through to a PDF, the formatting of the final document is not important at this stage. As you probably can tell, there will be some kind off header with serial, comany name and stuff like that. Following that is a table with details on the specific content of the package.

<?xml version="1.0" encoding="UTF-8" ?> 

<configuration>

  <sheet number="no2345" ordernumber="112233" customername="CompanyName" customernumber="1324" technician="Oscar" date="08-11-2010" notes="" /> 

  <component category="Hard-Drive" description="WD500GB 7200rpm" serialnumber="WDxxx" bios="1.3" firmware="1.1" note="Updated Bios" /> 

  <component category="Keyboard" description="HP 2.5GHz" serialnumber="HPxxx" bios="N/A" firmware="N/A" note="" /> 

</configuration>

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
If you make this your XSLT, you will see that fop will render some result, not really nice, but some result
For a better PDF, you will need some specs
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:fo="http://www.w3.org/1999/XSL/Format" 

    version="1.0">

    <xsl:output indent="yes"/>

    <xsl:template match="configuration">

        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema">

            <fo:layout-master-set>

                <fo:simple-page-master master-name="master" page-height="845.0pt" page-width="598.0pt" margin-top="0.0pt" margin-left="0.0pt" margin-bottom="0.0pt" margin-right="0.0pt">

                    <fo:region-body margin-left="45.0pt" margin-top="44.3pt" margin-bottom="40.0pt" margin-right="34.7pt"/>

                    <fo:region-before extent="44.3pt" precedence="true"/>

                    <fo:region-after extent="40.0pt" precedence="true"/>

                    <fo:region-start extent="45.0pt" precedence="false"/>

                    <fo:region-end extent="34.7pt" precedence="false"/>

                </fo:simple-page-master>

            </fo:layout-master-set>

            <fo:page-sequence master-reference="master" force-page-count="no-force">

                <fo:flow flow-name="xsl-region-body">

                    <fo:block-container >

                        <fo:block>

                            <fo:table>

                                <fo:table-body>

                                    <xsl:apply-templates select="component"/>

                                </fo:table-body>

                            </fo:table>

                        </fo:block>

                    </fo:block-container>

                </fo:flow>

            </fo:page-sequence>

        </fo:root>

    </xsl:template>

    <xsl:template match="component">

        <fo:table-row>

            <xsl:for-each select="@*">

                <fo:table-cell width="15%">

                    <fo:block>

                        <fo:inline>

                            <xsl:value-of select="."/>

                        </fo:inline>

                    </fo:block>

                </fo:table-cell>

            </xsl:for-each>

        </fo:table-row>

    </xsl:template>

</xsl:stylesheet>

Open in new window

0
 
LVL 1

Author Comment

by:nebb-tgr
Comment Utility
Did you try this with the document that I supplied? I'm getting the same error as before. What command are you using, and what version of fop?
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
FOP 0.9 whatever, yes it works.
"It does not work" is not really a message that can help me to debug what is going on.
Can you be more specific in what you experience?
0
 
LVL 1

Author Comment

by:nebb-tgr
Comment Utility
Well, I don't understand how you can use the exact same code as I am, without getting error messages.

Here is the error message I'm recieving. It seems to have something to do with the encoding, but I'm certainly not sure.

 
12.nov.2010 15:13:29 org.apache.fop.cli.Main startFOP

SEVERE: Exception

javax.xml.transform.TransformerException: Invalid byte 1 of 1-byte UTF-8 sequenc

e.

        at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:302)

        at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:130)

        at org.apache.fop.cli.Main.startFOP(Main.java:174)

        at org.apache.fop.cli.Main.main(Main.java:205)

Caused by: javax.xml.transform.TransformerException: Invalid byte 1 of 1-byte UT

F-8 sequence.

        at org.apache.xalan.transformer.TransformerImpl.fatalError(TransformerIm

pl.java:780)

        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp

l.java:756)

        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp

l.java:1284)

        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp

l.java:1262)

        at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299)

        ... 3 more



---------



; SystemID: file:/C:/xmltopdf/fop-1.0/PC.xml; Line#: 8; Column#: 55

javax.xml.transform.TransformerException: Invalid byte 1 of 1-byte UTF-8 sequenc

e.

        at org.apache.xalan.transformer.TransformerImpl.fatalError(TransformerIm

pl.java:780)

        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp

l.java:756)

        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp

l.java:1284)

        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImp

l.java:1262)

        at org.apache.fop.cli.InputHandler.transformTo(InputHandler.java:299)

        at org.apache.fop.cli.InputHandler.renderTo(InputHandler.java:130)

        at org.apache.fop.cli.Main.startFOP(Main.java:174)

        at org.apache.fop.cli.Main.main(Main.java:205)

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
Can you check that the in your files there is no whitespace before the xml declaration
(or anything else?)

The message says that one of the XML files is not valid (either the XSLT or the source XML)
I had both of them opened in an XML IDE prior to saving,
doing the same you made an error I believe
0
 
LVL 1

Author Comment

by:nebb-tgr
Comment Utility
The documents are copied and pasted into notepad and saved. I have tried saving both as ANSI and UTF-8 without success. There are noe whitespaces before the xml-declaration in either document.

Is there some known problem to making and editing XML in notepad?
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
can you attach your source XML as a file?
0
 
LVL 1

Author Comment

by:nebb-tgr
Comment Utility
Here it is.
 TestPc.xml

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
Comment Utility
Running with FOP 1.0 I had some issues with the table width.
Table processing is pretty poor in FOP
I changed the XSLT and now I get nice PDF,
even with FOP 1.0

This is my command-line
fop -xml TestPc.xml -xsl TestPc.xslt -pdf TestPC.pdf
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    xmlns:fo="http://www.w3.org/1999/XSL/Format" 

    version="1.0">

    <xsl:output indent="yes"/>

    <xsl:template match="configuration">

        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema">

            <fo:layout-master-set>

                <fo:simple-page-master master-name="master" page-height="845.0pt" page-width="598.0pt" margin-top="0.0pt" margin-left="0.0pt" margin-bottom="0.0pt" margin-right="0.0pt">

                    <fo:region-body margin-left="45.0pt" margin-top="44.3pt" margin-bottom="40.0pt" margin-right="34.7pt"/>

                    <fo:region-before extent="44.3pt" precedence="true"/>

                    <fo:region-after extent="40.0pt" precedence="true"/>

                    <fo:region-start extent="45.0pt" precedence="false"/>

                    <fo:region-end extent="34.7pt" precedence="false"/>

                </fo:simple-page-master>

            </fo:layout-master-set>

            <fo:page-sequence master-reference="master" force-page-count="no-force">

                <fo:flow flow-name="xsl-region-body">

                    <fo:block-container >

                        <fo:block>

                            <fo:table width="500pt">

                                <fo:table-body>

                                    <xsl:apply-templates select="component"/>

                                </fo:table-body>

                            </fo:table>

                        </fo:block>

                    </fo:block-container>

                </fo:flow>

            </fo:page-sequence>

        </fo:root>

    </xsl:template>

    <xsl:template match="component">

        <fo:table-row>

            <xsl:for-each select="@*">

                <fo:table-cell width="80pt">

                    <fo:block>

                        <fo:inline>

                            <xsl:value-of select="."/>

                        </fo:inline>

                    </fo:block>

                </fo:table-cell>

            </xsl:for-each>

        </fo:table-row>

    </xsl:template>

</xsl:stylesheet>

Open in new window

0
 
LVL 1

Author Closing Comment

by:nebb-tgr
Comment Utility
The solution is possibly very specific to my problem. But parts of the problem might be useful for others to review.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 47
computer science syllabus 3 52
Change to event 1 72
eclipse formatting 6 38
This article provides the solution to a question (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28674207.html) posed here at Experts Exchange. The asker of the question has many JPG images in many folders, and all of t…
Online collaboration can help businesses be more efficient, help employees grow their skills and foster a team environment.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now