How Could I transform XML into XSL without getting error, using JAVA xml API -
i tried convert xml file html using xslt format got error
rror: 'namespace prefix 'vuln' undeclared.' fatal error: 'could not compile stylesheet'
the xml file started
<?xml version='1.0' encoding='utf-8'?> <?xml-stylesheet type="text/xsl" version ="2.0" href="nvdxslt.xsl"?> <nvd xmlns:cpe-lang="http://cpe.mitre.org/language/2.0" xmlns:scapcore="http://scap.nist.gov/schema/scap-core/0.1" xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:patch="http://scap.nist.gov/schema/patch/0.1" xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0" > <entry id="cve-2007-5333"> <vuln:vulnerable-software-list> <vuln:product>cpe:/a:apache_software_foundation:tomcat:4.1.34</vuln:product> <vuln:product>cpe:/a:apache_software_foundation:tomcat:4.1.37</vuln:product> <vuln:product>cpe:/a:apache:tomcat:4.1.24</vuln:product> <vuln:product>cpe:/a:apache:tomcat:5.5.5</vuln:product> <vuln:product>cpe:/a:apache:tomcat:5.5.2</vuln:product> </vuln:vulnerable-software-list> <vuln:cve-id>cve-2007-5333</vuln:cve-id> <vuln:published-datetime>2008-02-11t20:00:00.000-05:00</vuln:published-datetime> <vuln:last-modified-datetime>2014-03-15t23:16:41.310-04:00</vuln:last-modified-datetime> </entry> </nvd>
and created xsl transformer transform xml content organized format , display output on html format javax.xml.transform api. xsl file follow:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:cvss="http://scap.nist.gov/schema/cvss-v2/0.2" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:patch="http://scap.nist.gov/schema/patch/0.1" xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0"> <xsl:output method="html" encoding="utf-8" indent="yes"/> <xsl:template match="/"> <!-- todo: auto-generated template --> <html> <body> <h1>parsing nvd xml file</h1> <table border="1"> <tr> <th>hasaffectedproducts</th> </tr> <xsl:for-each select="nvd/entry"> <tr> <td><xsl:for-each select="/vuln:vulnerable-software-list"><xsl:value-of select="vuln:product" /></xsl:for-each></td> </tr> <td><xsl:value-of select="vuln:cve-id" /></td> <td><xsl:value-of select="vuln:published-datetime" /></td> <td><xsl:value-of select="vuln:last-modified-datetime"/></td> .....
the java api used transformation
public void transform(string dataxml, string inputxsl, string outputhtml) throws transformerconfigurationexception, transformerexception { transformerfactory factory = transformerfactory.newinstance(); streamsource xslstream = new streamsource(inputxsl); transformer transformer = factory.newtransformer(xslstream); streamsource in = new streamsource(dataxml); streamresult out = new streamresult(outputhtml); transformer.transform(in, out); system.out.println("the generated html file is:" + outputhtml); }
simply, received xml file , xsl file , create output file int html format.
you have 2 issues stylesheet.
the first issue involves namespaces. need bind vuln
prefix same namespace uri in stylesheet in document, , need bind prefix http://scap.nist.gov/schema/feed/vulnerability/2.0
uri default namespace of document. unprefixed names in xpath expressions refer elements not in namespace, match elements in document default xmlns
must use prefix in xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:vuln="http://scap.nist.gov/schema/vulnerability/0.4" xmlns:feed="http://scap.nist.gov/schema/feed/vulnerability/2.0">
you should remove default namespace declaration xmlns="http://scap.nist.gov/schema/feed/vulnerability/2.0"
stylesheet put output elements in namespace rather treating them html.
with these namespace bindings in place can correct outer for-each
elements in right namespace
<xsl:for-each select="feed:nvd/feed:entry">
the second problem in inner for-each
<xsl:for-each select="feed:nvd/feed:entry"> <tr> <td><xsl:for-each select="/vuln:vulnerable-software-list">
where you're using absolute path should using relative 1 - you're trying for-each
on vulnerable-software-list
elements @ root level of document , print respective first product
child. transformer correctly giving empty node set root element called nvd
, in different namespace.
if instead want for-each
on all product
elements in current entry change to
<xsl:for-each select="vuln:vulnerable-software-list/vuln:product"> <xsl:value-of select="." /> </xsl:for-each>
Comments
Post a Comment