0

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>
But if any other format is easier to obtain, that would be fine as well.

Thank you!

8
  • You have two templates with the same match pattern. Only the last one of these is being applied. Since your 1st template applies templates to 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. Commented May 20, 2020 at 12:12
  • So which output do you want to create exactly? Show to minimal sample input files and the corresponding (HTML?) result you want to create/. Commented May 20, 2020 at 12:27
  • @MartinHonnen, I am trying to get an output similar to this: a header with just the text "Section names" and then lines with each section name. Ideally, it would have two columns, the additional one containing information about the file from which the section name was extracted. Commented May 20, 2020 at 12:37
  • Please edit your question and show the exact code your example should produce. Commented May 20, 2020 at 12:38
  • Hi @michael.hor257k , thank you very much for your answer. Both outputs you suggested are great and I can use them. However, my file does not get transformed. I do not get any error so I'm not sure what's wrong. I tried in Notepad++, using the "report.xml" file which is in the initial post. If I add the full path to firefox, all it shows is: "Section names". Do you have any idea what could be wrong? Commented May 20, 2020 at 13:13

1 Answer 1

1

I suspect you want to do something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/report">
    <html>
        <body>
            <h1>
                <xsl:value-of select="title"/>
            </h1>
            <xsl:for-each select="configfile">
                <h2>
                    <xsl:value-of select="@name"/>
                </h2>
                <xsl:for-each select="document(@name)/erpConnector/sections/section">
                    <p>
                        <xsl:value-of select="@name"/>
                    </p>
                </xsl:for-each>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

which in your example would produce a result like:

<html>
  <body>
    <h1>Section names</h1>
    <h2>myFirstXMLfile.xml</h2>
    <p>Section1</p>
    <p>Section2</p>
    <p>Section3</p>
    <h2>mySecondXMLfile.xml</h2>
    <p>Section1b</p>
    <p>Section2b</p>
    <p>Section3b</p>
  </body>
</html>

If your prefer to output a table, then try:

<xsl:template match="/report">
    <html>
        <body>
            <h1>
                <xsl:value-of select="title"/>
            </h1>
            <table>
                <tr>
                    <th>Section name</th>
                    <th>Filename</th> 
                </tr>
                <xsl:for-each select="configfile">
                    <xsl:variable name="filename" select="@name" />
                    <xsl:for-each select="document($filename)/erpConnector/sections/section">
                        <tr>
                            <td>
                                <xsl:value-of select="@name"/>
                            </td>
                            <td>
                                <xsl:value-of select="$filename"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </xsl:for-each>
            </table>    
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.