I have the following situation: out of multiple almost similar in structure xml files, I need to extract an attribute and the file from which it comes from.
I believe I am close to a solution, but cannot make it work. I am new to xml and xslt and would need some guidance. Thank you.
My multiple xml files are all in the same location and they have the following structure:
<erpConnector>
<sections>
<section name="Section1">
<!-- other irrelevant content here -->
</section>
<section name="Section2">
<!-- other irrelevant content here -->
</section>
<section name="Section3">
<!-- other irrelevant content here -->
</section>
</sections>
</erpConnector>
I created a separate xml file called "report.xml" which acts like an aggregator for all my other xml files. It looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="reports3.xsl" version="1.0"?>
<report>
<title>Section names</title>
<configfile name="C:\Users\.myFirstXMLfile.xml"/>
<configfile name="C:\Users\mySecondXMLfile.xml"/>
</report>
Lastly, I have the xsl file:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="/report/title"/></title>
</head>
<body>
<xsl:for-each select="/report/configfile">
<xsl:apply-templates select="document(@name)/erpConnector"/>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template match="/">
<h1>
<xsl:value-of select="erpConnector/sections/section/@name"/>
<xsl:text> </xsl:text>
</h1>
</xsl:template>
</xsl:stylesheet>
My output is just "<h1> <\h1>" .
Please let me know what could be the cause and how to address it.
The output could look like this:
<!DOCTYPE html>
<html>
<body>
<h2>Basic HTML Table</h2>
<table style="width:100%">
<tr>
<th>Section name</th>
<th>Filename</th>
</tr>
<tr>
<td>Section1</td>
<td>myfirstxml</td>
</tr>
<tr>
<td>Section2</td>
<td>mysecondxml</td>
</tr>
</table>
</body>
</html>
Thank you!
erpConnector, that's what your other template should match. -- Note that the template as currently written tries to output the name of the 1st section in the document - which is not what you said you wanted.