Link to home
Start Free TrialLog in
Avatar of nebb-tgr
nebb-tgr

asked on

XML to PDF, attributes with spaces

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.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Yes, spaces won't work
Avatar of nebb-tgr
nebb-tgr

ASKER

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?
You need to use names without spaces
I understand that, but that is not possible at this point. I need to be able to use the XML-documents as they are.
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>

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

That does not work for me. What other possibilites are there for converting XML to PDF other that through XSL?
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
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

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

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?
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?
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

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
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?
can you attach your source XML as a file?
Here it is.
 TestPc.xml

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The solution is possibly very specific to my problem. But parts of the problem might be useful for others to review.