Jump to content

Putting xml and xslt in one file


omegared
 Share

Recommended Posts

I am new to this so i would be grateful if someone could help me through this simple example. At the moment I have an xml file and a xsl(fo) file. I am feeding them to apache fop to create pdf. I now want to place the xml and xsl in one file whilst being able to use xpath references to the data model rather then the text values.Would someone be kind enough as to show me how to convert my existing hello world files to achieve:A)place both xml and xsl in one file(how would the new file look)Existing xml file:<?xml version="1.0" encoding="UTF-8"?><data> <param1>hello</param1> <param2>world</param2></data>Existing xsl file:<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:template match="/"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="0.2cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm"> <fo:region-body margin-top="2cm"/> <fo:region-before extent="3cm"/> <fo:region-after extent="1.5cm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="simple"> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates select="data"/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="data"> <fo:block> <xsl:apply-templates select="param1"/> <xsl:apply-templates select="param2"/> </fo:block> </xsl:template><xsl:template match="param1"> <fo:block font-size="12pt" font-family="sans-serif" space-after.optimum="30pt" text-align="justify"> <xsl:value-of select="."/> </fo:block></xsl:template><xsl:template match="param2"> <fo:block font-size="12pt" space-before.optimum="110pt" text-align="justify"> <xsl:value-of select="."/> </fo:block></xsl:template></xsl:stylesheet>Sincere thanks

Link to comment
Share on other sites

There is no "plain XML & XSLT" way to do that. You could use something like some complex JavaScript with(out) E4X, but it's very complicated and the only thing it does is to embed the files in one XHTML file. It doesn't change the way the XSLT treats the XML.If you need a data model (what's (not) allowed where) you should use DTD or better yet- Schema, to define those legal building blocks. If you use schema, you might be able to make the XSLT look into it as a data model reference.

Link to comment
Share on other sites

I posted this elsewhere and somebody gave a response but I don't understand it. Hes reply was: You can view the XSLT FAQ about the identity transformation: http://www.dpawson.co.uk/xsl/sect2/identity.html Basically, try something like that for the first transformation: <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:x_="http://www.fgeorges.org/Transform/Alias" version="2.0"> <xsl:namespace-alias stylesheet-prefix="x_" result-prefix="xsl"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="fo:*/text()"> <!-- Try to set a variable, instead. --> <x_:value-of select="doc('')/to-datas/{.}"/> </xsl:template> </xsl:transform> It will transform the fo:* elements by wrapping their text nodes into xsl:value-of elements. So you'll give an XSLT script. Then use this script as usual, to produce the FO. You'll have maybe to customize the template rule matching "/", depending on what your document looks like (is it an FO or XSLT document?, etcetera).If anybody understands this, would you be kind enough as to modify my hello world program to demonstrate how the file would be.Many thanks

Link to comment
Share on other sites

Hmm. That's a funny one. Never though it's possible. Well, accordingly to the code you gave earlyer, maybe something like this would do:

<?xml version="1.0"?><?xml-stylesheet type="text/xml" href="#stylesheet"?><!DOCTYPE data [<!ATTLIST xsl:stylesheet  id	ID	#REQUIRED>]><data><xsl:stylesheet id="stylesheet"                version="1.0"                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"                xmlns:fo="http://www.w3.org/1999/XSL/Format">  <!-- any xsl:import elements if you later on get any -->  <xsl:template match="xsl:stylesheet" /><xsl:template match="/"><fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"><fo:layout-master-set><fo:simple-page-master master-name="simple"page-height="29.7cm"page-width="21cm"margin-top="0.2cm"margin-bottom="2cm"margin-left="2.5cm"margin-right="2.5cm"><fo:region-body margin-top="2cm"/><fo:region-before extent="3cm"/><fo:region-after extent="1.5cm"/></fo:simple-page-master></fo:layout-master-set><fo:page-sequence master-reference="simple"><fo:flow flow-name="xsl-region-body"><xsl:apply-templates select="data"/></fo:flow></fo:page-sequence></fo:root></xsl:template><xsl:template match="data"><fo:block><xsl:apply-templates select="param1"/><xsl:apply-templates select="param2"/></fo:block> </xsl:template><xsl:template match="param1"><fo:block font-size="12pt"font-family="sans-serif"space-after.optimum="30pt"text-align="justify"><xsl:value-of select="."/></fo:block></xsl:template><xsl:template match="param2"><fo:block font-size="12pt"space-before.optimum="110pt"text-align="justify"><xsl:value-of select="."/></fo:block></xsl:template></xsl:stylesheet><param1>hello</param1><param2>world</param2></data>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...