I want to translate an XML file with data like the following:
<FlatData>
<Details1_Collection>
<Details1 Customer1="Customer" Total1="3" />
...
</Details1_Collection>
</FlatData>
The data I am interested in is the attributes and their values in each Details1. The problem is that these attributes are not necessarily going to be the same in every XML file I want to translate, and I want a general purpose XSL that could handle such Details1 as these:
<Details1 Customer1="Customer" Total1="3" />
<Details1 Name="Jim" Age="14" Weight="180" />
<Details1 Date="2009-07-27" Range="1-5" Option1="True" />
These different Details1 would not occur in the same source XML file, but rather in different files. However, I would like to use the same XSL on each.
I was thinking I needed something like <xsl:value-of select="@attribute_name"/> but what do I put for @attribute_name when I don’t know beforehand what attributes there will be? Also, how do I capture the attribute name? I would like to explode the source XML above to something like:
<Details1>
<Customer1>Customer</Customer1>
<Total1>3</Total1>
</Details1>
Edit: thanks for the responses! I’m having trouble getting more than the following output, however:
<?xml version="1.0" encoding="UTF-8"?>
<FlatData>
<Details1_Collection></Details1_Collection>
</FlatData>
I’ve tried both lavinio’s and Jörn Horstmann’s answers, as well as trying to combine the two. I run this command:
msxsl.exe -o output.xml input.xml transform.xsl
I think something that’s getting in the way is a namespace in the input file:
<Report Name="MyReport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="MyReport">
There was increased difficulty because of the Microsoft SQL Reporting Services 2008 namespace that was part of the input XML. I didn’t realize at first that
<Report Name="MyReport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="MyReport">was such an important line. Thanks to Pavel Minaev for the namespace comment. The following XSL worked to extract the data I wanted:I think I will try to clean this up to use the
apply-templatesstyle that lavinio suggested. Thanks also to Jörn Horstmann for theselect="@*"code infor-eachloops. It would be interesting to figure out why Reporting Services reports are dumped initially with thexmlnsvalue set to the name of the report, and not a schema URL.I’ll continue to update this answer as I refine this XSL.
Edit: here’s a namespace-agnostic version since, for each different report from Reporting Services, there will apparently be a different namespace: