Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

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.
0
nebb-tgr
Asked:
nebb-tgr
  • 9
  • 8
  • 2
1 Solution
 
CEHJCommented:
Yes, spaces won't work
0
 
nebb-tgrAuthor Commented:
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
 
CEHJCommented:
You need to use names without spaces
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nebb-tgrAuthor Commented:
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
 
Geert BormansCommented:
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
 
Geert BormansCommented:
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
 
nebb-tgrAuthor Commented:
That does not work for me. What other possibilites are there for converting XML to PDF other that through XSL?
0
 
Geert BormansCommented:
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
 
nebb-tgrAuthor Commented:
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
 
Geert BormansCommented:
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
 
nebb-tgrAuthor Commented:
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
 
Geert BormansCommented:
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
 
nebb-tgrAuthor Commented:
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
 
Geert BormansCommented:
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
 
nebb-tgrAuthor Commented:
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
 
Geert BormansCommented:
can you attach your source XML as a file?
0
 
nebb-tgrAuthor Commented:
Here it is.
 TestPc.xml

0
 
Geert BormansCommented:
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
 
nebb-tgrAuthor Commented:
The solution is possibly very specific to my problem. But parts of the problem might be useful for others to review.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now