XSLT improvement

2019-08-01 16:17发布

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 resultfrom 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>&#xa;</xsl:text>
        <xsl:value-of select="cdf:title"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="key('result', @idref)/result"/>
        <xsl:text>&#xa;</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.

标签: xml xslt
2条回答
手持菜刀,她持情操
2楼-- · 2019-08-01 16:34

You have several issues:

  1. TestResult is not the root element, so your template:

    <xsl:template match="/cdf:TestResult">
    

    is never applied. The output that you see is produced purely by the built-in template rules.

  2. TestResult has no title child, so your instruction:

    <xsl:value-of select="cdf:title"/>
    

    will not return anything.

  3. When you do:

    <xsl:for-each select="cdf:rule-result">   
    

    you are already in the context of rule-result, so your use of a key makes no sense. And you forgot to include the cdf: prefix on the result.


Based on your previous question, I would think you want to go over the Rule nodes under Group and get the linked rule-result from there, instead of going over rule-result nodes directly:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:cdf="http://checklists.nist.gov/xccdf/1.2">
<xsl:output method="text" encoding="utf-8" />

<xsl:key name="result" match="cdf:rule-result" use="@idref" />

<xsl:template match="/cdf:Benchmark">
    <xsl:for-each select="cdf:Group/cdf:Rule">
        <xsl:value-of select="@id" />
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="cdf:title"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="key('result', @id)/cdf:result"/>
        <xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, the result will be:

xccdf_com.vmware.linux_rule_test-def-3
Rule 3 - /etc/passwd file is group-owned by root
pass
xccdf_com.vmware.linux_rule_test-def-2
Rule 2 - /etc/passwd file is owned by root
pass
xccdf_com.vmware.linux_rule_test-def-1
Rule 1 - /etc/passwd file has permissions of 644 or more restrictive
pass
查看更多
够拽才男人
3楼-- · 2019-08-01 16:39

First, to filter you result, just match the root node and apply only your desired node. such as

<xsl:template match="/">
    <xsl:apply-templates select="cdf:Benchmark/cdf:TestResult"/>
</xsl:template>

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 get cdf:result, just select it directly, thus:

<xsl:template match="cdf:TestResult">
    <xsl:for-each select="cdf:rule-result">
        <xsl:value-of select="@idref" />
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="preceding-sibling::cdf:title"/>
        <xsl:text>&#xa;</xsl:text>
        <xsl:value-of select="cdf:result"/>
        <xsl:text>&#xa;</xsl:text>
    </xsl:for-each>
</xsl:template>

and the whole stylesheet:

<?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:template match="/">
        <xsl:apply-templates select="cdf:Benchmark/cdf:TestResult"/>
    </xsl:template>

    <xsl:template match="cdf:TestResult">
        <xsl:for-each select="cdf:rule-result">
            <xsl:value-of select="@idref" />
            <xsl:text>&#xa;</xsl:text>
            <xsl:value-of select="preceding-sibling::cdf:title"/>
            <xsl:text>&#xa;</xsl:text>
            <xsl:value-of select="cdf:result"/>
            <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

outputs:

xccdf_com.vmware.linux_rule_test-def-3
OSCAP Scan Result
pass
xccdf_com.vmware.linux_rule_test-def-2
OSCAP Scan Result
pass
xccdf_com.vmware.linux_rule_test-def-1
OSCAP Scan Result
pass
查看更多
登录 后发表回答