簡體   English   中英

基於命令行參數的XSL修改

[英]XSL Modify Based on Command Line Param

我有以下XML摘錄。 完整的XML是OVF定義。

<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:cim="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vmw="http://www.vmware.com/schema/ovf" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x xml:lang="en-US">
      <Item>
        <rasd:Caption ovf:msgid="network.eth0.caption"/>
        <rasd:Connection>eth0</rasd:Connection>
        <rasd:Description ovf:msgid="network.eth0.description"/>
        <rasd:ElementName>eth0</rasd:ElementName>
        <rasd:InstanceID>13</rasd:InstanceID>
        <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType>
        <rasd:ResourceType>10</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:Caption ovf:msgid="network.eth1.caption"/>
        <rasd:Connection>eth1</rasd:Connection>
        <rasd:Description ovf:msgid="network.eth1.description"/>
        <rasd:ElementName>eth1</rasd:ElementName>
        <rasd:InstanceID>14</rasd:InstanceID>
        <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType>
        <rasd:ResourceType>10</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:Caption ovf:msgid="network.eth2.caption"/>
        <rasd:Connection>eth2</rasd:Connection>
        <rasd:Description ovf:msgid="network.eth2.description"/>
        <rasd:ElementName>eth2</rasd:ElementName>
        <rasd:InstanceID>15</rasd:InstanceID>
        <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType>
        <rasd:ResourceType>10</rasd:ResourceType>
      </Item>
      <Item>
        <rasd:Caption ovf:msgid="network.eth3.caption"/>
        <rasd:Connection>eth3</rasd:Connection>
        <rasd:Description ovf:msgid="network.eth3.description"/>
        <rasd:ElementName>eth3</rasd:ElementName>
        <rasd:InstanceID>16</rasd:InstanceID>
        <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType>
        <rasd:ResourceType>10</rasd:ResourceType>
      </Item>     
</Envelope>

我正在嘗試在<rasd:Connection>eth*</rasd:Connection>行之前插入<rasd:AutomaticAllocation>false</rasd:AutomaticAllocation> <rasd:Connection>eth*</rasd:Connection>行,但不是全部<rasd:Connection>eth*</rasd:Connection> 到目前為止,我已經獲得了以下XSL並可以正常工作,但問題是我必須對要禁用的每個接口進行硬編碼。

<xsl:template match="rasd:Connection[text()='eth0']">
    <xsl:if test="$disableEths='true'">
        <xsl:element name="rasd:AutomaticAllocation">false</xsl:element>
        <xsl:copy-of select="."/>
    </xsl:if>
</xsl:template> 
<xsl:template match="rasd:Connection[text()='eth1']">
    <xsl:if test="$disableEths='true'">
        <xsl:element name="rasd:AutomaticAllocation">false</xsl:element>
        <xsl:copy-of select="."/>
    </xsl:if>
</xsl:template> 
<xsl:template match="rasd:Connection[text()='eth2']">
    <xsl:if test="$disableEths='true'">
        <xsl:element name="rasd:AutomaticAllocation">false</xsl:element>
        <xsl:copy-of select="."/>
    </xsl:if>
</xsl:template>

有沒有一種方法可以讓用戶傳入包含要禁用的值的定界列表的參數,如果沒有輸入任何參數,則不要禁用其中的任何一個? 如果重要,請使用xsltproc作為處理器。

如評論中所建議,用戶是否應生成一個XML文件,例如如下方式命名的DisableItems.xml (順便說一下,該文件可以使用常規方法從文本分隔文件,.txt,.csv,.tab等生成。語言:C#,Java,Perl,PHP,Python,R,VB ...):

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <disableitem>eth1</disableitem>
  <disableitem>eth2</disableitem>
  <disableitem>eth3</disableitem>  
</root>

然后,XSLT可以使用其document()函數進行相應的搜索。 確保其他xml文件與原始源xml位於同一目錄中:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
               xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

  <!-- Identity Transform -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>  

  <xsl:template match="rasd:Connection[text()=document('DisableItems.xml')/root/disableitem]">
    <xsl:element name="rasd:AutomaticAllocation">false</xsl:element>
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:transform>

輸出(注意,在查找xml中未指定的eth0沒有false元素)

<?xml version='1.0' encoding='UTF-8'?>
<Envelope xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:cim="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vmw="http://www.vmware.com/schema/ovf" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:lang="en-US">
  <Item>
    <rasd:Caption ovf:msgid="network.eth0.caption"/>
    <rasd:Connection>eth0</rasd:Connection>
    <rasd:Description ovf:msgid="network.eth0.description"/>
    <rasd:ElementName>eth0</rasd:ElementName>
    <rasd:InstanceID>13</rasd:InstanceID>
    <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType>
    <rasd:ResourceType>10</rasd:ResourceType>
  </Item>
  <Item>
    <rasd:Caption ovf:msgid="network.eth1.caption"/>
    <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
    <rasd:Connection>eth1</rasd:Connection>
    <rasd:Description ovf:msgid="network.eth1.description"/>
    <rasd:ElementName>eth1</rasd:ElementName>
    <rasd:InstanceID>14</rasd:InstanceID>
    <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType>
    <rasd:ResourceType>10</rasd:ResourceType>
  </Item>
  <Item>
    <rasd:Caption ovf:msgid="network.eth2.caption"/>
    <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
    <rasd:Connection>eth2</rasd:Connection>
    <rasd:Description ovf:msgid="network.eth2.description"/>
    <rasd:ElementName>eth2</rasd:ElementName>
    <rasd:InstanceID>15</rasd:InstanceID>
    <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType>
    <rasd:ResourceType>10</rasd:ResourceType>
  </Item>
  <Item>
    <rasd:Caption ovf:msgid="network.eth3.caption"/>
    <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
    <rasd:Connection>eth3</rasd:Connection>
    <rasd:Description ovf:msgid="network.eth3.description"/>
    <rasd:ElementName>eth3</rasd:ElementName>
    <rasd:InstanceID>16</rasd:InstanceID>
    <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType>
    <rasd:ResourceType>10</rasd:ResourceType>
  </Item>
</Envelope>

如果使用字符串分隔的值列表以字符串形式傳遞--stringparam ,則使用xsltproc可以使用EXSLT str:tokenize函數,如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0"
  xmlns:str="http://exslt.org/strings"
  exclude-result-prefixes="str"
  xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData">

<xsl:output indent="yes"/>

<xsl:param name="disableEths" select="'true'"/>
<xsl:param name="con-to-change" select="'eth0,eth1,eth2'"/>

<xsl:template match="@* | node()" name="identity"> 
  <xsl:copy> 
    <xsl:apply-templates select="@* | node()"/> 
  </xsl:copy> 
</xsl:template> 

<xsl:template match="rasd:Connection">
  <xsl:choose>
    <xsl:when test=". = str:tokenize($con-to-change, ',') and $disableEths='true'">
        <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation>
        <xsl:copy-of select="."/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="identity"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template> 

</xsl:stylesheet>

當然,希望直接在匹配模式中使用參數,但是在XSLT 1.0中不允許在匹配模式中使用變量或參數引用,我認為xsltproc允許,但是在具有match="rasd:Connection[. = str:tokenize($con-to-change, ',')]"的測試中match="rasd:Connection[. = str:tokenize($con-to-change, ',')]"我收到一些有關未定義變量的錯誤消息,因此上述建議將檢查移入模板。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM