XSLT:将通配符掩码转换为网络掩码和网络掩码的格式

时间:2018-08-21 16:04:52

标签: xml xslt xpath

我有一个具有以下结构的XML输入:

<address>
    <ip>192.168.7.5</ip>
    <netmask>16</netmask>
    <wildcard-mask>0.0.0.255</wildcard-mask>
    <enable-wildcard-mask>false</enable-wildcard-mask>
</address>

我想创建一个XSLT转换,该转换接受此输入并使用  如果“ enable-wildcard-mask”为true,则为“ wildcard-mask”;如果“ enable-wildcard-mask”为false,则为“ netmask”。 但是,必须将网络掩码从此格式“ 24”转换为“ 255.255.255.0”,并且必须将通配符掩码转换为网络掩码(二进制取反)。

因此,此XSLT的输出应类似于:

<netmask>255.255.255.0</netmask>

如果“ enable-wildcard-mask”为真,并且

<netmask>255.255.0.0</netmask>

如果“ enable-wildcard-mask”为假。

这是用于测试的基本XSLT代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="address">
    <xsl:choose>
        <xsl:when test="wildcard-mask and (enable-wildcard-mask/text()='true')">
            <netmask type="string"><xsl:value-of select="wildcard-mask"/></netmask>
        </xsl:when>
        <xsl:when test="netmask">
            <netmask type="string"><xsl:value-of select="netmask"/></netmask>
        </xsl:when>
        <xsl:otherwise>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

如何在XSLT中进行必要的按位转换?

1 个答案:

答案 0 :(得分:0)

最后,我使用查找表实现了转换,因为每个表只有32个条目。

因此,第一个查询表将通配符掩码转换为网络掩码,而第二个表将“ / 24”形式的网络掩码转换为255.255.255.0。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="address">
    <xsl:choose>
        <!-- These 2 look-up tables convert a wildcard to a netmask, and a netmask in this form '/24' to '255.255.255.0', respectively -->
        <xsl:when test="wildcard-mask and (enable-wildcard-mask/text()='true')">
            <xsl:choose>
                <xsl:when test="(wildcard-mask/text()='255.255.255.255')"><netmask type="string">0.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='127.255.255.255')"><netmask type="string">128.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='63.255.255.255')"><netmask type="string">192.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='31.255.255.255')"><netmask type="string">224.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='15.255.255.255')"><netmask type="string">240.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='7.255.255.255')"><netmask type="string">248.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='3.255.255.255')"><netmask type="string">252.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='1.255.255.255')"><netmask type="string">254.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.255.255.255')"><netmask type="string">255.0.0.0</netmask></xsl:when>

                <xsl:when test="(wildcard-mask/text()='0.255.255.255')"><netmask type="string">255.0.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.127.255.255')"><netmask type="string">255.128.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.63.255.255')"><netmask type="string">255.192.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.31.255.255')"><netmask type="string">255.224.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.15.255.255')"><netmask type="string">255.240.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.7.255.255')"><netmask type="string">255.248.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.3.255.255')"><netmask type="string">255.252.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.1.255.255')"><netmask type="string">255.254.0.0</netmask></xsl:when>

                <xsl:when test="(wildcard-mask/text()='0.0.255.255')"><netmask type="string">255.255.0.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.127.255')"><netmask type="string">255.255.128.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.63.255')"><netmask type="string">255.255.192.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.31.255')"><netmask type="string">255.255.224.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.15.255')"><netmask type="string">255.255.240.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.7.255')"><netmask type="string">255.255.248.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.3.255')"><netmask type="string">255.255.252.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.1.255')"><netmask type="string">255.255.254.0</netmask></xsl:when>

                <xsl:when test="(wildcard-mask/text()='0.0.0.255')"><netmask type="string">255.255.255.0</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.0.127')"><netmask type="string">255.255.255.128</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.0.63')"><netmask type="string">255.255.255.192</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.0.31')"><netmask type="string">255.255.255.224</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.0.15')"><netmask type="string">255.255.255.240</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.0.7')"><netmask type="string">255.255.255.248</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.0.3')"><netmask type="string">255.255.255.252</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.0.1')"><netmask type="string">255.255.255.254</netmask></xsl:when>
                <xsl:when test="(wildcard-mask/text()='0.0.0.0')"><netmask type="string">255.255.255.255</netmask></xsl:when>
                <xsl:otherwise></xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:when test="netmask">
            <xsl:choose>
                <xsl:when test="(netmask/text()='0')"><netmask type="string">0.0.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='1')"><netmask type="string">128.0.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='2')"><netmask type="string">192.0.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='3')"><netmask type="string">224.0.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='4')"><netmask type="string">240.0.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='5')"><netmask type="string">248.0.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='6')"><netmask type="string">252.0.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='7')"><netmask type="string">254.0.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='8')"><netmask type="string">255.0.0.0</netmask></xsl:when>

                <xsl:when test="(netmask/text()='9')"><netmask type="string">255.128.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='10')"><netmask type="string">255.192.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='11')"><netmask type="string">255.224.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='12')"><netmask type="string">255.240.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='13')"><netmask type="string">255.248.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='14')"><netmask type="string">255.252.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='15')"><netmask type="string">255.254.0.0</netmask></xsl:when>

                <xsl:when test="(netmask/text()='16')"><netmask type="string">255.255.0.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='17')"><netmask type="string">255.255.128.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='18')"><netmask type="string">255.255.192.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='19')"><netmask type="string">255.255.224.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='20')"><netmask type="string">255.255.240.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='21')"><netmask type="string">255.255.248.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='22')"><netmask type="string">255.255.252.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='23')"><netmask type="string">255.255.254.0</netmask></xsl:when>

                <xsl:when test="(netmask/text()='24')"><netmask type="string">255.255.255.0</netmask></xsl:when>
                <xsl:when test="(netmask/text()='25')"><netmask type="string">255.255.255.128</netmask></xsl:when>
                <xsl:when test="(netmask/text()='26')"><netmask type="string">255.255.255.192</netmask></xsl:when>
                <xsl:when test="(netmask/text()='27')"><netmask type="string">255.255.255.224</netmask></xsl:when>
                <xsl:when test="(netmask/text()='28')"><netmask type="string">255.255.255.240</netmask></xsl:when>
                <xsl:when test="(netmask/text()='29')"><netmask type="string">255.255.255.248</netmask></xsl:when>
                <xsl:when test="(netmask/text()='30')"><netmask type="string">255.255.255.252</netmask></xsl:when>
                <xsl:when test="(netmask/text()='31')"><netmask type="string">255.255.255.254</netmask></xsl:when>
                <xsl:when test="(netmask/text()='32')"><netmask type="string">255.255.255.255</netmask></xsl:when>
                <xsl:otherwise></xsl:otherwise>
            </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>