我正在尝试使用XSL文件从XML文档中提取特定值。我试图拉的值是在关键节点下,其中关键必须包含值cimhttpserver。如果它包含的值我想继续打印信息,否则什么也不打印。我试着搜索了一下,这是我所能找到的。如果你看到我的错误,请帮助我。
文件运行良好,直到我开始调整它以返回特定的值;现在它执行了,但是什么也没有返回。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:audit="urn:internalvim25">
<xsl:template match="audit:returnval">
<xsl:for-each select="audit:propSet/audit:val[@xsi:type='HostConfigInfo']/audit:firewall/audit:ruleset/audit:key">
<xsl:if test="key[text()='CIMHttpsServer']">
Key <xsl:value-of select="audit:key"/>
Label <xsl:value-of select="audit:label"/>
Required <xsl:value-of select="audit:required"/>
Rule
-Port <xsl:value-of select="audit:rule/audit:port"/>
-Direction <xsl:value-of select="audit:rule/audit:direction"/>
-Protocol <xsl:value-of select="audit:rule/audit:protocol"/>
Enabled <xsl:value-of select="audit:enabled"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这是正在使用的XML文件的一个片段。
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<RetrievePropertiesResponse xmlns="urn:internalvim25">
<returnval>
<obj type="HostSystem" serverGuid="165246FF-D519-4F4F-A52E-1AF1D0866BA7">host-28</obj>
<propSet>
<name>alarmActionsEnabled</name>
<val xsi:type="xsd:boolean">true</val>
</propSet>
<propSet>
<name>availableField</name>
<val xsi:type="ArrayOfCustomFieldDef" />
</propSet>
<propSet>
<name>capability</name>
<val xsi:type="HostCapability">
<recursiveResourcePoolsSupported>true</recursiveResourcePoolsSupported>
<cpuMemoryResourceConfigurationSupported>true</cpuMemoryResourceConfigurationSupported>
<rebootSupported>true</rebootSupported>
<shutdownSupported>true</shutdownSupported>
<vmotionSupported>true</vmotionSupported>
<standbySupported>true</standbySupported>
<ipmiSupported>true</ipmiSupported>
<maxSupportedVMs>1200</maxSupportedVMs>
<maxRunningVMs>0</maxRunningVMs>
<maxSupportedVcpus>8</maxSupportedVcpus>
<datastorePrincipalSupported>true</datastorePrincipalSupported>
<sanSupported>true</sanSupported>
<nfsSupported>true</nfsSupported>
<iscsiSupported>true</iscsiSupported>
<vlanTaggingSupported>true</vlanTaggingSupported>
<nicTeamingSupported>true</nicTeamingSupported>
<highGuestMemSupported>true</highGuestMemSupported>
<maintenanceModeSupported>true</maintenanceModeSupported>
<suspendedRelocateSupported>true</suspendedRelocateSupported>
<restrictedSnapshotRelocateSupported>true</restrictedSnapshotRelocateSupported>
<perVmSwapFiles>true</perVmSwapFiles>
<localSwapDatastoreSupported>true</localSwapDatastoreSupported>
<unsharedSwapVMotionSupported>true</unsharedSwapVMotionSupported>
<backgroundSnapshotsSupported>false</backgroundSnapshotsSupported>
<preAssignedPCIUnitNumbersSupported>true</preAssignedPCIUnitNumbersSupported>
<screenshotSupported>true</screenshotSupported>
<scaledScreenshotSupported>true</scaledScreenshotSupported>
<storageVMotionSupported>true</storageVMotionSupported>
<vmotionWithStorageVMotionSupported>false</vmotionWithStorageVMotionSupported>
<recordReplaySupported>false</recordReplaySupported>
<ftSupported>false</ftSupported>
<replayUnsupportedReason>incompatibleCpu</replayUnsupportedReason>
<ftCompatibilityIssues>haAgentIssue</ftCompatibilityIssues>
<ftCompatibilityIssues>incompatibleCpu</ftCompatibilityIssues>
<ftCompatibilityIssues>missingFTLoggingNic</ftCompatibilityIssues>
<ftCompatibilityIssues>missingVMotionNic</ftCompatibilityIssues>
<loginBySSLThumbprintSupported>true</loginBySSLThumbprintSupported>
<cloneFromSnapshotSupported>true</cloneFromSnapshotSupported>
<deltaDiskBackingsSupported>true</deltaDiskBackingsSupported>
<perVMNetworkTrafficShapingSupported>false</perVMNetworkTrafficShapingSupported>
<tpmSupported>false</tpmSupported>
<virtualExecUsageSupported>true</virtualExecUsageSupported>
<storageIORMSupported>true</storageIORMSupported>
<vmDirectPathGen2Supported>false</vmDirectPathGen2Supported>
<vmDirectPathGen2UnsupportedReason>hostNptIncompatibleHardware</vmDirectPathGen2UnsupportedReason>
<vmDirectPathGen2UnsupportedReason>hostNptDisabled</vmDirectPathGen2UnsupportedReason>
<vStorageCapable>true</vStorageCapable>
</val>
</propSet>
<propSet>
<name>config</name>
<val xsi:type="HostConfigInfo">
...<snip>...
<firewall>
<defaultPolicy>
<incomingBlocked>false</incomingBlocked>
<outgoingBlocked>true</outgoingBlocked>
</defaultPolicy>
<ruleset>
<key>CIMHttpServer</key>
<label>CIM Server</label>
<required>false</required>
<rule>
<port>5988</port>
<direction>inbound</direction>
<protocol>tcp</protocol>
</rule>
<enabled>true</enabled>
</ruleset>
<ruleset>
<key>CIMHttpsServer</key>
<label>CIM Secure Server</label>
<required>false</required>
<rule>
<port>5989</port>
<direction>inbound</direction>
<protocol>tcp</protocol>
</rule>
<enabled>true</enabled>
</ruleset>
所需输出:Key CIMHttpsServer
Label CIM Secure Server
Required false
Rule
-Port 5989
-Direction inbound
-Protocol tcp
Enabled true
你的方法太复杂了。试试更模块化的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:audit="urn:internalvim25"
exclude-result-prefixes="audit xsi soap"
>
<xsl:output type="text" />
<xsl:variable name="NL" select="'
'" />
<xsl:template match="/">
<xsl:apply-templates select=".//*[audit:key = 'CIMHttpsServer']" />
</xsl:template>
<xsl:template match="audit:ruleset">
<xsl:value-of select="concat('Key: ', audit:key, $NL)" />
<xsl:value-of select="concat('Label: ', audit:label, $NL)" />
<xsl:value-of select="concat('Required: ', audit:required, $NL)" />
<xsl:apply-templates select="audit:rule" />
<xsl:value-of select="concat('Enabled: ', audit:enabled, $NL)"/>
</xsl:template>
<xsl:template match="audit:rule">
<xsl:value-of select="concat('Rule', $NL)" />
<xsl:value-of select="concat('-Port: ', audit:port, $NL)" />
<xsl:value-of select="concat('-Direction: ', audit:direction, $NL)" />
<xsl:value-of select="concat('-Protocol: ', audit:protocol, $NL)" />
</xsl:template>
</xsl:stylesheet>
指出- 避免输出"unprotected"文本。要么像我一样用
<xsl:value-of>
,要么用<xsl:text>
。你的源代码看起来会干净很多(因为你可以正确地开始缩进),你也会更容易格式化输出。 - 使用模板匹配。不要把整个处理逻辑塞进一个有N个嵌套层的大模板中。
- 如果要输出纯文本,请使用适当的输出模式。
- 使用
exclude-result-prefixes
,如果你不输出纯文本,并希望保持输入名称空间出现在输出。不过,始终启用该功能也无妨。 - 您不需要声明不打算使用的名称空间。
soap
和xsi
对您的样式表没有影响,您可以省略它们。
我建议您开始在XSLT中使用Identity模板。如果需要,可以使用以下XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:audit="urn:internalvim25">
<xsl:output method="text" encoding="UTF-8" />
<xsl:variable name="lf" select="' '" />
<xsl:template match="@*|node()">
<xsl:apply-templates select="@*|node()" />
</xsl:template>
<xsl:template match="audit:ruleset[audit:key = 'CIMHttpsServer']">
<xsl:text>Key </xsl:text><xsl:value-of select="audit:key"/><xsl:value-of select="$lf" />
<xsl:text>Label </xsl:text><xsl:value-of select="audit:label"/><xsl:value-of select="$lf" />
<xsl:text>Required </xsl:text><xsl:value-of select="audit:required"/><xsl:value-of select="$lf" />
<xsl:text>Rule</xsl:text><xsl:value-of select="$lf" />
<xsl:text>-Port </xsl:text><xsl:value-of select="audit:rule/audit:port"/><xsl:value-of select="$lf" />
<xsl:text>-Direction </xsl:text><xsl:value-of select="audit:rule/audit:direction"/><xsl:value-of select="$lf" />
<xsl:text>-Protocol </xsl:text><xsl:value-of select="audit:rule/audit:protocol"/><xsl:value-of select="$lf" />
<xsl:text>Enabled </xsl:text><xsl:value-of select="audit:enabled"/><xsl:value-of select="$lf" />
</xsl:template>
</xsl:stylesheet>
这将产生所需的输出。如果您不想使用单位变换并保持XSLT更像您现在所拥有的,我建议删除xsl:if
并向xsl:for-each
添加测试,如下所示:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:audit="urn:internalvim25">
<xsl:template match="audit:returnval">
<xsl:for-each select="audit:propSet/audit:val[@xsi:type='HostConfigInfo']/audit:firewall/audit:ruleset[audit:key = 'CIMHttpsServer']">
Key <xsl:value-of select="audit:key"/>
Label <xsl:value-of select="audit:label"/>
Required <xsl:value-of select="audit:required"/>
Rule
-Port <xsl:value-of select="audit:rule/audit:port"/>
-Direction <xsl:value-of select="audit:rule/audit:direction"/>
-Protocol <xsl:value-of select="audit:rule/audit:protocol"/>
Enabled <xsl:value-of select="audit:enabled"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>