XSLT 改进 [英] XSLT improvement

查看:12
本文介绍了XSLT 改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与 XSLT 可以进一步改进吗?有关.>

我有一个如下的 XML:

示例 XML

<status date="2015-09-20">草稿</status><title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">file_test 探针的测试内容</title><描述 xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">OVAL 文件的文件内容 file_test_content-oval.xml 建立于 2015-09-20T02:13:56</描述><platform idref="cpe:/o:sles11:linux"/><version>v0.0</version><model system="urn:xccdf:scoring:default"/><配置文件 id="xccdf_com.vmware.linux_profile_test"><status date="2015-09-20">草稿</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">这是用于测试 file_test 探针的测试配置文件.</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"/></个人资料><组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">规则 3 -/etc/passwd 文件归根组所有</title><description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">此规则验证/etc/passwd 文件是由 root 组拥有的.</说明><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"/></检查></规则><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">规则 2 -/etc/passwd 文件由 root 拥有 </title><description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">此规则验证/etc/passwd文件是否归root所有.</说明><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"/></检查></规则><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">规则 1 -/etc/passwd 文件的权限为 644 或更多限制</标题><description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">此规则验证/etc/passwd 文件具有 644 或更多限制的权限.</描述><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"/></检查></规则></组><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 扫描结果</title><身份验证=假"特权=假"/><profile idref="xccdf_com.vmware.linux_profile_test"/><target>vROPS_6-1</target><目标地址>127.0.0.1</目标地址><目标地址>127.0.0.2</目标地址><目标地址>10.112.56.130</目标地址><目标地址>0:0:0:0:0:0:0:1</目标地址><目标地址>fe80:0:0:0:250:56ff:fe93:6159</目标地址><目标地址>0:0:0:0:0:0:0:10.112.56.130</目标地址><目标地址>0:0:0:0:0:0:0:127.0.0.2</目标地址><目标地址>0:0:0:0:0:0:0:127.0.0.1</目标地址><目标事实><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></目标事实><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"><结果>通过</结果><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"/></检查></规则结果><rule-result idref="xccdf_com.vmware.linux_rule_test-def-2" time="2016-08-17T13:38:29" weight="1.000000"><结果>通过</结果><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"/></检查></规则结果><rule-result idref="xccdf_com.vmware.linux_rule_test-def-1" time="2016-08-17T13:38:29" weight="1.000000"><结果>通过</结果><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"/></检查></规则结果><score system="urn:xccdf:scoring:default" maximum="100.000000">100.000000</score></测试结果></基准>

请注意,Group 和 Profile 元素是可选.一个有效的示例 xml 也可以如下所示:

<status date="2015-09-20">草稿</status><title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">file_test 探针的测试内容</title><描述 xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">OVAL 文件的文件内容 file_test_content-oval.xml 建立于 2015-09-20T02:13:56</描述><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">规则 3 -/etc/passwd 文件归根组所有</title><description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">此规则验证/etc/passwd 文件是由 root 组拥有的.</说明><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"/></检查></规则><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">规则 2 -/etc/passwd 文件由 root 拥有 </title><description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">此规则验证/etc/passwd文件是否归root所有.</说明><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"/></检查></规则><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">规则 1 -/etc/passwd 文件的权限为 644 或更多限制</标题><description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">此规则验证/etc/passwd 文件具有 644 或更多限制的权限.</描述><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"/></检查></规则><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 扫描结果</title><身份验证=假"特权=假"/><target>vRealizeClusterNode</target><目标地址>127.0.0.1</目标地址><目标地址>127.0.0.2</目标地址><目标地址>10.112.56.132</目标地址><目标地址>0:0:0:0:0:0:0:1</目标地址><目标地址>fe80:0:0:0:250:56ff:fe93:21b6</目标地址><目标事实><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></目标事实><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"><结果>通过</结果><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"/></检查></规则结果><rule-result idref="xccdf_com.vmware.linux_rule_test-def-2" time="2016-08-17T19:18:42" weight="1.000000"><结果>通过</结果><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"/></检查></规则结果><rule-result idref="xccdf_com.vmware.linux_rule_test-def-1" time="2016-08-17T19:18:42" weight="1.000000"><结果>通过</结果><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"/></检查></规则结果><score system="urn:xccdf:scoring:default" maximum="100.000000">100.000000</score></测试结果></基准>

我有兴趣从上述两个可能的 xml 中获取 idref,以及相应的 titleresult.

输出可能只是由 \n 分隔的纯文本.

我写的 XSL 如下:

但是,这并没有产生所需的输出?我在这里做错了什么?

请帮忙.

解决方案

您有几个问题:

  1. TestResult 不是根元素,所以你的模板:

    从不应用.您看到的输出纯粹是由内置模板规则.

  2. TestResult 没有 title 孩子,所以你的指令:

    不会返回任何东西.

  3. 当你这样做时:

    您已经处于 rule-result 的上下文中,因此您使用密钥是没有意义的.您忘记在 result 中包含 cdf: 前缀.

<小时>

根据您之前的问题,我认为您想查看 Group 下的 Rule 节点并获取链接的 rule-result从那里开始,而不是直接遍历 rule-result 节点:

XSLT 1.0

<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:模板></xsl:stylesheet>

应用于您的输入示例,结果将是:

xccdf_com.vmware.linux_rule_test-def-3规则 3 -/etc/passwd 文件归 root 所有经过xccdf_com.vmware.linux_rule_test-def-2规则 2 -/etc/passwd 文件归 root 所有经过xccdf_com.vmware.linux_rule_test-def-1规则 1 -/etc/passwd 文件的权限为 644 或更多限制经过

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.

解决方案

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

这篇关于XSLT 改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆