Solved

XML to PDF, attributes with spaces

Posted on 2010-11-11
19
376 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
ID: 34114441
Yes, spaces won't work
0
 
LVL 1

Author Comment

by:nebb-tgr
ID: 34114619
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
ID: 34114643
You need to use names without spaces
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:nebb-tgr
ID: 34114693
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
ID: 34115327
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
ID: 34115352
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
ID: 34120433
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
ID: 34120495
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
ID: 34120659
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 34121201
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
ID: 34130590
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
ID: 34131582
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
ID: 34134241
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
ID: 34134324
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
ID: 34134370
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
ID: 34134642
can you attach your source XML as a file?
0
 
LVL 1

Author Comment

by:nebb-tgr
ID: 34135063
Here it is.
 TestPc.xml

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 34135196
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
ID: 34135363
The solution is possibly very specific to my problem. But parts of the problem might be useful for others to review.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

PaperPort 14.5 Patch 1 update is often not detected or downloaded automatically. This article provides direct download links to solve the problem for retail (non-bundled) versions of the Standard and Professional editions, as well as the Professiona…
When the confidentiality and security of your data is a must, trust the highly encrypted cloud fax portfolio used by 12 million businesses worldwide, including nearly half of the Fortune 500.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

776 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