如果xsl中的条件不起作用,则为多个

时间:2018-07-24 06:30:06

标签: python xml xslt

以下是我的XML文件:

<?xml version='1.0' encoding='ASCII'?>
<default>
    <dut name='Dut'>
        <var val='customer' >ABC</var>
        <var val='dpi' >Disable</var>
        <var val='icr' >Enable</var>
        <var val='trap' >Disable</var>
    </dut>
</default>

这是我的XSL文件的一部分:

<xsl:variable name="author"
              select="'Shreyans'"/>
Author:  <xsl:text/>
   <xsl:value-of select="$author" />


<xsl:if test="default/dut[@name='Dut']/var='customer'">
    Doing some customer config
</xsl:if>

<xsl:if test="default/dut[@name='Dut']/var='ABC' and default/dut[@name='Dut']/var/@val='customer'">
    # ------------------------ customer is ABC ------------------------

    .if $(customer) == ABC {
    echo "ABC customer config is enabled"
    .endif }
</xsl:if>
<xsl:if test="default/dut[@name='Dut']/var='DEF' and default/dut[@name='Dut']/var/@val='customer'">
    # ------------------------ customer is DEF ------------------------

    .if $(customer) == DEF {
    echo "DEF customer config is enabled"
    .endif }
</xsl:if>
<xsl:if test="default/dut[@name='Dut']/var='Enable' and default/dut[@name='Dut']/var/@val='icr'">
    # ------------------------ icr is enabled ------------------------

    .if $(icr) == True {
    echo "icr is enabled"
        <xsl:if test="(default/dut[@name='Dut']/var='Enable') and (default/dut[@name='Dut']/var/@val='trap')">
        .if $(trap) == True {
            echo "trap is enabled"
        .endif }
        </xsl:if>
    .endif }
</xsl:if>
<xsl:if test="default/dut[@name='Dut']/var='Enable' and default/dut[@name='Dut']/var/@val='dpi'">
    # ------------------------ dpi is enabled ------------------------

    .if $(dpi) == True {
    echo "dpi is enabled"
    .endif }
</xsl:if>

以下是我的简短python脚本-

import os
import lxml.etree as ET

xmlPath = '/tmp/xmlTemp.xml'
xslPath = '/tmp/xslTemp.xsl'
outfile = '/tmp/run_time_config_file'

dom = ET.parse(xmlPath)
xslt = ET.parse(xslPath)
transform = ET.XSLT(xslt)
newdom = transform(dom)
print(ET.tostring(newdom, pretty_print=True))

cmd = "xsltproc {} {} > {}".format(xslPath, xmlPath, outfile)
os.system(cmd)

由于某种原因,我的多个条件未生效。 从我的XML文件中,只有'icr'是'Enable',但是如果条件检查为'Enable'的所有内容(在这种情况下为“ icr”和“ dpi”)都会被打印出来。

有人可以指出我的错误吗?预先感谢!

我看到以下输出:

Author:  Shreyans
    # ------------------------ customer is ABC ------------------------

    .if $(customer) == ABC {
    echo "ABC customer config is enabled"
    .endif }

    # ------------------------ icr is enabled ------------------------
.if $(icr) == True {
echo "icr is enabled"

    .if $(trap) == True {
        echo "trap is enabled"
    .endif }

.endif }

# ------------------------ dpi is enabled ------------------------

.if $(dpi) == True {
echo "dpi is enabled"
.endif }

0 个答案:

没有答案