Include XML files via Aspose?

We have a number of reports which have the same header and footer, so we would like to reduce the code-duplication by INCLUDING the XML files somehow, perhaps like this:

pdf.AppendXML(header.xml);
pdf.AppendXML(content.xml);
pdf.AppendXML(footer.xml);
pdf.BindXML();

Is there a way to INCLUDE xml files like this in order to avoid the code duplication?

Thanks,

Edward

Dear Edward,

Thank you for considering Aspose.

Do you mean the XML files you appended are parts of a completed XML? For example:

[header.xml]
<Pdf>
    <Section>
        <Header>
           ...
        </Header>
[content.xml]
        <Text>...</Text>
        ...
[footer.xml]
        <Footer>
        ...
        </Footer>
    </Section>
</Pdf>


Yes, that’s it, or to be more exact, like this:

<Pdf> 
  <Section> 
    <Header> 
      [header.xml] 
    </Header> 
    [content.xml] 
    <Footer> 
      [footer.xml] 
    </Footer> 
  </Section> 
</Pdf> 

Is there anyway to include XML files like this using Aspose?

Thanks,

Edward

Dear Edward,

Thank you for considering Aspose.

You can use XSLT to merge XML files before binding it to Aspose.Pdf. Here is an example:

[test.xslt]

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/">
	<Pdf xmlns="Aspose.Pdf">
		<Section>
			<Header>
				<xsl:copy-of select="document('header.xml')"/>
			</Header>
			<xsl:copy-of select="document('content.xml')"/>
		</Section>
	</Pdf>
</xsl:template>	
</xsl:stylesheet>

[header.xml]

<Text>
	<Segment>header</Segment>
</Text>

[content.xml]

<Text>
	<Segment>Hello world</Segment>
</Text>

[C# code]

XmlDocument xmlDoc = new XmlDocument();

MemoryStream ms = new MemoryStream();

XslTransform xsl = new XslTransform();
xsl.Load(“test.xslt”);
xsl.Transform(xmlDoc,null,ms);

ms.Position = 0;
xmlDoc.Load(ms);
ms.Close();

Pdf pdf = new Pdf();
pdf.BindXML(xmlDoc,null);

pdf.Save(“e:/temp/test.pdf”);

[VisualBasic code]

Dim xmlDoc As XmlDocument = New XmlDocument()

Dim ms As MemoryStream = New MemoryStream()

Dim xsl As XslTransform = New XslTransform()
xsl.Load(“test.xslt”)
xsl.Transform(xmlDoc,Nothing,ms)

ms.Position = 0
xmlDoc.Load(ms)
ms.Close()

Dim pdf As Pdf = New Pdf()
pdf.BindXML(xmlDoc,Nothing)

pdf.Save(“e:/temp/test.pdf”)

When merging a new attribute “xmlns” will be added into the root elements of the incluing XML files which will cause an exception. So please download the latest hot fix to run this example.

The above code works as long as I don’t have any tags with ID attributes in my included XML file. But if I have an ID attribute in my included XML file like this:

<Text> 
  <Segment ID="TableData"/> 
</Text> 

And try to reference it from my code, then I get the error:

Unknown attribute in Table element. The attribute name is xmlnsTableData. Why is it now putting an “xmlns” in front of the ID name? How do I reference these nodes now that they are in the INCLUDED xml file?

Thanks,
Edward

XSL file:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
	<Pdf xmlns="Aspose.Pdf">
		<Section ID="Section1" PageMarginLeft="50" PageMarginRight="0" PageMarginTop="120" PageMarginBottom="60" IsLandscape="true">
			<Header  IsFirstPageOnly="false" MarginTop="50">
				<xsl:copy-of select="document('Report_Header_Landscape.xml')"/>				
			</Header>					
			<Table ID="MultiviewTable"></Table>		
		</Section>
	</Pdf>
</xsl:template>
</xsl:stylesheet>

XML: file

<?xml version="1.0" encoding="UTF-8" ?>
<Table ID="TableData">	
	<Row>
		<Cell FitWidth="90">
			<Image Type="jpeg" File="C:\Inetpub\wwwroot\sivasm\SM_WEB\Common\img\homepic_vvo.jpg"/>
		</Cell>
		<Cell FitWidth="300">
			<Text>
				<Segment FontName="Arial" FontSize="14">Company GmbH</Segment>
			</Text>
		</Cell>	
		<Cell FitWidth="346">
			<Text>
				<Segment FontName="Arial" ID="ReportTitle" IsTrueTypeFontBold="True" FontSize="14" Alignment="Right">[REPORT TITLE]</Segment>
			</Text>
		</Cell>	
	</Row>
</Table>

Dear Edward,

Thank you for considering Aspose.

I haven’t found any error in your code. You can use code like the following to save the resulting XML and check what it looks like:

XmlDocument xmlDoc = new XmlDocument();

FileStream ms = new FileStream(“result.xml”,FileMode.Create);

XslTransform xsl = new XslTransform();
xsl.Load(“test.xslt”);
xsl.Transform(xmlDoc,null,ms);

In fact, I am not very familiar with XSLT, it is not a part of Aspose.Pdf. You can search newsgroups or forums about XSLT to get more help.

I finally found a way that works. I changed:

<Pdf xmlns="Aspose.Pdf">
to:
<Pdf>
and everything works fine.

Thanks,

Edward

Dear EDward,

Thank you for considering Aspose.

That’s great. Thanks for your info.

There is still one issue with this code that I am trying to work out. It compiles and executes ok, but …

It gives the following warning for this line:
xsl.Transform(xmlDoc,null,ms);

‘Public Sub Transform(input As System.Xml.XPath.IXPathNavigable, args As System.Xml.Xsl.XsltArgumentList, output As System.IO.Stream)’ is obsolete:
'You should pass XmlResolver to Transform() method’

I’ve been trying various ways to get around this using XmlResolver but can’t get the right syntax.

Does anyone know how to construct this line so that it doesn’t throw a warning at compile-time?

Thanks,
Edward


Hi Edward

here is a function that shows how to make use of the newer XslCompiledTransform structure
Remember that it is also only a warning as something to get fixed eventually, not an error.

Function Strip_RawValue(ByVal xmlDoc As XmlDocument) As XmlDocument
Dim new_xmlDoc As New XmlDocument
Dim output As New System.Text.StringBuilder
Dim writer As New System.IO.StringWriter(output)
Dim xsl As New XslCompiledTransform
xsl.Load(“[http://theServer/XSL/strip_raw-value.xsl](http://theserver/XSL/strip_raw-value.xsl)”)
xsl.Transform(xmlDoc, Nothing, writer)
new_xmlDoc.LoadXml(output.ToString)
Strip_RawValue = ReduceElementNames(new_xmlDoc)
End Function

Hope this helps. As per normal, this code is supplied as is, where is, no responsiblity etc. There may well be better ways to do this.
Write back if you have any further questions

Regards

Ralph Price