Saturday, 31 January 2015

Parsing nested XML File as node set using XSLT



I have a problem. I have an xml file as below :


Main.xml



<msrsw>
<software>
<chapter>
<name> Hello world</sample>
<xref type="xml">C:\ABC\NestedXML.xml</xref>
</chapter>
</software>
</msrsw>


I am using Apache FOP to produce PDF document (Using Java). FOP needs src and dest params. Src being Main.xml. My XSLT is as below:



<xsl:param name="xmlFileName" />
<xsl:param name="XMLFile" select="document($xmlFileName)"/>
<xsl:template name="Chapters_1_2_Template">
<xsl:apply-templates select="$XMLFile/*" mode="chapter" />
</xsl:template>

<xsl:template match="node()" mode="name">
<xsl:value-of select="current()" />
</xsl:template>

<xsl:template match="node()" mode="chapter">
<xsl:for-each select="node()">
<xsl:if test="current()[name() = 'xref']">
<xsl:apply-templates select="current()[name() = 'xref']" mode="x" />
</xsl:if>
</xsl:for-each>
</xsl:template>

<xsl:template match="xref" mode="x">
<xsl:param name="XMLFile" select="document(current())"/>
<xsl:call-template name="Chapters_1_2_Template" />
</xsl:template>


When I execute the above code, i get the following error in the line


Invalid token XMLFile.


When I remove $XMLFile - <xsl:apply-templates select="*" mode="chapter" /> , it works fine for me with Main.xml file's content. When I just display the value of current() in xref template, it is parses the NestedXml.xml file and displayes string values in PDF.


My requirement is to embed the node-set of NestedXML.xml within the<xref></xref> tags and recursively applyTemplates on that node-set. But the document() function gives me the parsed String content of NestedXml.xml. Please suggest me where am I going wrong. Is this approach right? Is there any other way to do this? Or it is not possible to do in this way using XSLT and that XSLT allows including only the parsed String values??


Thanks in Advance. Krut


No comments:

Post a Comment