This is linked with Can XSLT be improved further?.
I have an XML as below:
Sample XML
<?xml version="1.0" encoding="UTF-8"?>
<Benchmark xmlns="http://checklists.nist.gov/xccdf/1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="xccdf_com.vmware.linux_benchmark_file-test" resolved="1" xml:lang="en-US">
<status date="2015-09-20">draft</status>
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Test content for file_test probe</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">File content for OVAL file file_test_content-oval.xml built on 2015-09-20T02:13:56</description>
<platform idref="cpe:/o:sles11:linux"/>
<version>v0.0</version>
<model system="urn:xccdf:scoring:default"/>
<Profile id="xccdf_com.vmware.linux_profile_test">
<status date="2015-09-20">draft</status>
<version update="1" time="2015-09-20T14:57:39.808+05:30">1.0</version>
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Test_Profile</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en">This is a test profile to test file_test probe.</description>
<select idref="xccdf_com.vmware.linux_rule_test-def-3" selected="true"/>
<select idref="xccdf_com.vmware.linux_rule_test-def-2" selected="true"/>
<select idref="xccdf_com.vmware.linux_rule_test-def-1" selected="true"/>
</Profile>
<Group id="xccdf_com.vmware.linux_group_test" weight="1.000000">
<platform idref="cpe:/o:sles11:linux"/>
<Rule id="xccdf_com.vmware.linux_rule_test-def-3" selected="true" weight="1.000000" role="full" severity="unknown">
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Rule 3 - /etc/passwd file is group-owned by root</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">This rule verifies that /etc/passwd file is group-owned by root.</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:3" href="file_test_content-oval.xml"/>
</check>
</Rule>
<Rule id="xccdf_com.vmware.linux_rule_test-def-2" selected="true">
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Rule 2 - /etc/passwd file is owned by root</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">This rule verifies that /etc/passwd file is owned by root.</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:2" href="file_test_content-oval.xml"/>
</check>
</Rule>
<Rule id="xccdf_com.vmware.linux_rule_test-def-1" selected="true">
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Rule 1 - /etc/passwd file has permissions of 644 or more restrictive</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">This rule verifies that /etc/passwd file has permissions of 644 or more restrictive.</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:1" href="file_test_content-oval.xml"/>
</check>
</Rule>
</Group>
<TestResult id="xccdf_org.open-scap_testresult_xccdf_com.vmware.linux_profile_test" start-time="2016-08-17T13:38:29" end-time="2016-08-17T13:38:29" version="v0.0">
<benchmark href="/tmp/tmp.VnFIMSRKhw/input.xml" id="xccdf_com.vmware.linux_benchmark_file-test"/>
<title>OSCAP Scan Result</title>
<identity authenticated="false" privileged="false"/>
<profile idref="xccdf_com.vmware.linux_profile_test"/>
<target>vROPS_6-1</target>
<target-address>127.0.0.1</target-address>
<target-address>127.0.0.2</target-address>
<target-address>10.112.56.130</target-address>
<target-address>0:0:0:0:0:0:0:1</target-address>
<target-address>fe80:0:0:0:250:56ff:fe93:6159</target-address>
<target-address>0:0:0:0:0:0:0:10.112.56.130</target-address>
<target-address>0:0:0:0:0:0:0:127.0.0.2</target-address>
<target-address>0:0:0:0:0:0:0:127.0.0.1</target-address>
<target-facts>
<fact name="urn:xccdf:fact:scanner:name" type="string">OpenSCAP</fact>
<fact name="urn:xccdf:fact:scanner:version" type="string">1.2.5</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:50:56:93:61:59</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:50:56:93:61:59</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
</target-facts>
<platform idref="cpe:/o:sles11:linux"/>
<rule-result idref="xccdf_com.vmware.linux_rule_test-def-3" role="full" time="2016-08-17T13:38:29" severity="unknown" weight="1.000000">
<result>pass</result>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:3" href="file_test_content-oval.xml"/>
</check>
</rule-result>
<rule-result idref="xccdf_com.vmware.linux_rule_test-def-2" time="2016-08-17T13:38:29" weight="1.000000">
<result>pass</result>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:2" href="file_test_content-oval.xml"/>
</check>
</rule-result>
<rule-result idref="xccdf_com.vmware.linux_rule_test-def-1" time="2016-08-17T13:38:29" weight="1.000000">
<result>pass</result>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:1" href="file_test_content-oval.xml"/>
</check>
</rule-result>
<score system="urn:xccdf:scoring:default" maximum="100.000000">100.000000</score>
</TestResult>
</Benchmark>
Please note that Group and Profile element are optional. A valid sample xml could be one like below as well:
<?xml version="1.0" encoding="UTF-8"?>
<Benchmark xmlns="http://checklists.nist.gov/xccdf/1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="xccdf_com.vmware.linux_benchmark_file-test" resolved="1" xml:lang="en-US">
<status date="2015-09-20">draft</status>
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Test content for file_test probe</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">File content for OVAL file file_test_content-oval.xml built on 2015-09-20T02:13:56</description>
<platform idref="cpe:/o:sles11:linux"/>
<version>v0.0</version>
<model system="urn:xccdf:scoring:default"/>
<Rule id="xccdf_com.vmware.linux_rule_test-def-3" selected="true" weight="1.000000" role="full" severity="unknown">
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Rule 3 - /etc/passwd file is group-owned by root</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">This rule verifies that /etc/passwd file is group-owned by root.</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:3" href="file_test_content-oval.xml"/>
</check>
</Rule>
<Rule id="xccdf_com.vmware.linux_rule_test-def-2" selected="true">
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Rule 2 - /etc/passwd file is owned by root</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">This rule verifies that /etc/passwd file is owned by root.</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:2" href="file_test_content-oval.xml"/>
</check>
</Rule>
<Rule id="xccdf_com.vmware.linux_rule_test-def-1" selected="true">
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Rule 1 - /etc/passwd file has permissions of 644 or more restrictive</title>
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">This rule verifies that /etc/passwd file has permissions of 644 or more restrictive.</description>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:1" href="file_test_content-oval.xml"/>
</check>
</Rule>
<TestResult id="xccdf_org.open-scap_testresult_default-profile" start-time="2016-08-17T19:18:42" end-time="2016-08-17T19:18:42" version="v0.0">
<benchmark href="/tmp/tmp.nMXPWBmAJO/input.xml" id="xccdf_com.vmware.linux_benchmark_file-test"/>
<title>OSCAP Scan Result</title>
<identity authenticated="false" privileged="false"/>
<target>vRealizeClusterNode</target>
<target-address>127.0.0.1</target-address>
<target-address>127.0.0.2</target-address>
<target-address>10.112.56.132</target-address>
<target-address>0:0:0:0:0:0:0:1</target-address>
<target-address>fe80:0:0:0:250:56ff:fe93:21b6</target-address>
<target-facts>
<fact name="urn:xccdf:fact:scanner:name" type="string">OpenSCAP</fact>
<fact name="urn:xccdf:fact:scanner:version" type="string">1.2.5</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:50:56:93:21:B6</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:00:00:00:00:00</fact>
<fact name="urn:xccdf:fact:ethernet:MAC" type="string">00:50:56:93:21:B6</fact>
</target-facts>
<platform idref="cpe:/o:sles11:linux"/>
<rule-result idref="xccdf_com.vmware.linux_rule_test-def-3" role="full" time="2016-08-17T19:18:42" severity="unknown" weight="1.000000">
<result>pass</result>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:3" href="file_test_content-oval.xml"/>
</check>
</rule-result>
<rule-result idref="xccdf_com.vmware.linux_rule_test-def-2" time="2016-08-17T19:18:42" weight="1.000000">
<result>pass</result>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:2" href="file_test_content-oval.xml"/>
</check>
</rule-result>
<rule-result idref="xccdf_com.vmware.linux_rule_test-def-1" time="2016-08-17T19:18:42" weight="1.000000">
<result>pass</result>
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
<check-content-ref name="oval:com.vmware.test.linux:def:1" href="file_test_content-oval.xml"/>
</check>
</rule-result>
<score system="urn:xccdf:scoring:default" maximum="100.000000">100.000000</score>
</TestResult>
</Benchmark>
I am interested in picking up idref
, and corresponding title
and result
from both the above possible xmls.
The out put could be just plain text separated by \n
.
I wrote XSL as below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cdf="http://checklists.nist.gov/xccdf/1.2"
xmlns:exsl="http://exslt.org/common"
xmlns:db="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://docbook.org/ns/docbook"
xmlns:s="http://open-scap.org/"
exclude-result-prefixes="xsl cdf db s exsl"
xmlns:ovalres="http://oval.mitre.org/XMLSchema/oval-results-5"
xmlns:sceres="http://open-scap.org/page/SCE_result_file"
>
<xsl:output method="text" encoding="utf-8" />
<xsl:key name="result" match="cdf:rule-result" use="@idref" />
<xsl:template match="/cdf:TestResult">
<xsl:for-each select="cdf:rule-result">
<xsl:value-of select="@idref" />
<xsl:text>
</xsl:text>
<xsl:value-of select="cdf:title"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="key('result', @idref)/result"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
But, this isn't generating the desired output? What am I doing wrong here?
Please help.
You have several issues:
TestResult
is not the root element, so your template:is never applied. The output that you see is produced purely by the built-in template rules.
TestResult
has notitle
child, so your instruction:will not return anything.
When you do:
you are already in the context of
rule-result
, so your use of a key makes no sense. And you forgot to include thecdf:
prefix on theresult
.Based on your previous question, I would think you want to go over the
Rule
nodes underGroup
and get the linkedrule-result
from there, instead of going overrule-result
nodes directly:XSLT 1.0
Applied to your input example, the result will be:
First, to filter you result, just match the root node and apply only your desired node. such as
Second, you need to know about xpath axes (forward and backward).
cdf:title
is a preceding-sibling to the context node (cdf:rule-result
). Third, you don't need keys to getcdf:result
, just select it directly, thus:and the whole stylesheet:
outputs: