0

I need some help transforming a XML-File into a CSV.

Here's an excerpt of my XML-file how it looks like:

<?xml version='1.0' encoding='UTF-8'?>
<nvd xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2" xmlns:cpe-lang="http://cpe.mitre.org/language/2.0" xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4" xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0" xmlns:patch="http://scap.nist.gov/schema/patch/0.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:scap-core="http://scap.nist.gov/schema/scap-core/0.1" nvd_xml_version="2.0" pub_date="2013-04-12T12:00:30" xsi:schemaLocation="http://scap.nist.gov/schema/patch/0.1 http://nvd.nist.gov/schema/patch_0.1.xsd http://scap.nist.gov/schema/scap-core/0.1 http://nvd.nist.gov/schema/scap-core_0.1.xsd http://scap.nist.gov/schema/feed/vulnerability/2.0 http://nvd.nist.gov/schema/nvd-cve-feed_2.0.xsd">
  <entry id="CVE-2012-5048">
    <vuln:vulnerable-configuration id="http://nvd.nist.gov/">
      <cpe-lang:logical-test negate="false" operator="OR">
        <cpe-lang:fact-ref name="cpe:/a:optimalog:optima_plc:1.5.2"/>
        <cpe-lang:fact-ref name="cpe:/a:optimalog:optima_plc:1.5.1"/>
      </cpe-lang:logical-test>
    </vuln:vulnerable-configuration>
    <vuln:vulnerable-software-list>
      <vuln:product>cpe:/a:optimalog:optima_plc:1.4.10</vuln:product>
      <vuln:product>cpe:/a:optimalog:optima_plc:1.5.0</vuln:product>
    </vuln:vulnerable-software-list>
    <vuln:cve-id>CVE-2012-5048</vuln:cve-id>
    <vuln:published-datetime>2012-09-28T06:40:22.507-04:00</vuln:published-datetime>
    <vuln:last-modified-datetime>2013-04-10T23:31:27.227-04:00</vuln:last-modified-datetime>
    <vuln:cvss>
      <cvss:base_metrics>
        <cvss:score>7.8</cvss:score>
        <cvss:access-vector>NETWORK</cvss:access-vector>
      </cvss:base_metrics>
    </vuln:cvss>
    <vuln:cwe id="CWE-399"/>
    <vuln:references xml:lang="en" reference_type="UNKNOWN">
      <vuln:source>MISC</vuln:source>
      <vuln:reference href="http://www.us-cert.gov" xml:lang="en">http://www.us-cert.gov</vuln:reference>
    </vuln:references>
    <vuln:references xml:lang="en" reference_type="UNKNOWN">
      <vuln:source>BID</vuln:source>
      <vuln:reference href="http://www.securityfocus.com/bid/55712" xml:lang="en">55712</vuln:reference>
    </vuln:references>
    <vuln:summary>APIFTP Server</vuln:summary>
  </entry>
 </nvd>

Below you will find the XSLT-file I tried but it does not work properly as it does not write (for example) cvss:score and cvss:access-vector into different columns. Can anyone help me to write all entrys of the XML-file into different columns?

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>

<xsl:strip-space elements="*" />

<xsl:template match="/*/child::*">
<xsl:for-each select="child::*">
    <xsl:if test="position() != last()"><xsl:value-of select="normalize-space(.)"/>,</xsl:if>
    <xsl:if test="position()  = last()"><xsl:value-of select="normalize-space(.)"/><xsl:text>&#xD;</xsl:text>
    </xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
4
  • 1
    You need to show us a sample CSV you want to create for your posted XML sample or at least you need to explain in some words which XML elements and/or attributes in your XML map to lines and which ones to columns in the CSV. Commented Apr 13, 2013 at 11:22
  • How can I link a CSV here? Commented Apr 13, 2013 at 11:27
  • Insert it inline as a code sample, just as you have done for the XML and XSLT code. Commented Apr 13, 2013 at 11:55
  • It seems to be that I cannot paste codes in here, that's why I answered below. Commented Apr 13, 2013 at 15:15

1 Answer 1

1

What you are doing is:

<xsl:template match="/*/child::*">

Selecting any second level node, what is nvd/entry. in your example. Than running to any child of this:

<xsl:for-each select="child::*">

This are all third level nodes in your example. And selecting any text which is in any lower level node.

"select="normalize-space(.)"

As already mentioned by @ Martin Honnen is not clear what you try to do. But some comments To select text form the current node only, use:

"select="text()"; 

Also I do not think it's a good idea to generate the fields of an CVS file based on the position of elements in an xml file. The order off element in xml can change and is still correct, but this will destroy your CVS.

Update: Possible solution according the more detailed request:

<xsl:template match="*[name()='entry']">

    <xsl:value-of select="@id"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="*/vuln:product[1]"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="vuln:cvss/cvss:base_metrics/cvss:score"/>
    <xsl:text>;</xsl:text>
    <xsl:value-of select="vuln:references[vuln:source='MISC']/vuln:reference"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="vuln:references[vuln:source='BID']/vuln:reference"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="vuln:published-datetime" />
    <xsl:text>&#xD;</xsl:text>
</xsl:template> 

The stylesheet declaration should be something like:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2"
 xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4">
Sign up to request clarification or add additional context in comments.

2 Comments

Tunc Jamgocyan: I'm again a little bit confused. You are saying you get TransformerConfigurationException but the script is running anyway? Perhaps namespace declaration for "vuln" and "cvss" is missing. (But I only use xsl version 1.0. Also what do you mean with "display the value of each child element in a separate table cell"? I thought you try to generate a CVS file The cvs cells are currently separated with semicolon ";".
With this stylesheet declaration I was able to create the CSV file! Thank you. :-) What I meant with "display the value of each child element in a separate table cell" was a csv file containing all values of all existing elements separated through a semicolon (as an example). Like this: id;fact-ref;product;cve-id;published-datetime;last-modified-datetime;score;access-vector;source;reference;summary CVE-2012-5048;cpe:/a:optimalog:optima_plc:1.5.2,cpe:/a:optimalog:optima_plc:1.5.1;cpe:/a:optimalog:optima_plc:1.4.10,cpe:/a:optimalog:optima_plc:1.5.0;CVE-2012-5048; [...] ; APIFTP Server

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.