2017-02-24 61 views
-1

我有两个假设条件XSLT,如下图所示:动态条件执行XSLT

<xsl:if test="ns2:RoadPart/ns2:Vehicles/ns3:SemiVehicle>// for semi vehicle 
//code to show values from XML(ie 20,20) 
</xsl:if> 

<xsl:if test="ns2:RoadPart/ns2:Vehicles/ns3:NonsemiVehicle>// for non-semivehicle 
//code to show values from XML(ie 10,10) 
</xsl:if> 

我的XML如下:

// For Non semi vehicle 
<RoutePart Id="374941"> 
<Vehicles> 
<NonsemiVehicle> 
<OverallLengthListPosition xmlns="http://www.esdal.com/schemas/core/vehicle"> 
       <OverallLength> 
       <IncludingProjections>10</IncludingProjections> 
       <ExcludingProjections>10</ExcludingProjections> 
       </OverallLength> 
      </OverallLengthListPosition> 
</NonsemiVehicle> 
</Vehicles> 
</RoutePart> 

// For Semi vehicle 
<RoutePart Id="374941"> 
<Vehicles> 
<SemiVehicle> 
<OverallLengthListPosition xmlns="http://www.esdal.com/schemas/core/vehicle"> 
       <OverallLength> 
       <IncludingProjections>20</IncludingProjections> 
       <ExcludingProjections>20</ExcludingProjections> 
       </OverallLength> 
      </OverallLengthListPosition> 
</SemiVehicle> 
</Vehicles> 
</RoutePart> 

当我分析这个XSLT每次SemiVehicle列第一和非排名车辆排名第二。我每次都要按照给定的XML动态地调用它。

请帮助我。

编辑XSLT细节:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:stylesheet version="1.0" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ns1="http://test/schemas/core/proposedroute" 
    xmlns:ns2="http://test/schemas/core/movement" 
    xmlns:ns3="http://test/schemas/core/vehicle" 
    xmlns:ns4="http://test/schemas/core/commontypes" 
    xmlns:ns5="http://test/schemas/core/route" 
    xmlns:ns6="http://test/schemas/core/drivinginstruction" 
    xmlns:ns7="http://test/schemas/core/formattedtext" 
    xmlns:ns8="http://test/people/bs7666" 
    xmlns:ns9="http://test/schemas/core/annotation" 
    xmlns:ns10="http://test/schemas/core/caution" 
    xmlns:ns11="http://test/schemas/common/movementversion" 
    xmlns:ns12="http://test/schemas/core/contact"> 

    <xsl:param name="Contact_ID"></xsl:param> 
    <xsl:param name="UnitType"></xsl:param> 

    <xsl:param name="DocType"></xsl:param> 
    <xsl:template match="/ns1:Proposal"> 

    <html> 
     <body style="font-family:Arial;font-size:13px;"> 

     <br/> 
     <b>Provisional Route</b> 



     <!--Change Code Start RM#4604 21 july--> 
     <xsl:variable name="FirstVehicle"> 

      <xsl:for-each select="ns2:RouteParts/ns2:RoutePartListPosition/ns2:RoutePart/ns2:RoadPart/ns2:Vehicles"> 
      <xsl:if test="ns3:ConfigurationSummaryListPosition/ns3:ConfigurationSummary != ''"> 
       <xsl:if test="position()=1"> 
       <xsl:choose> 
        <xsl:when test="contains(ns3:ConfigurationSummaryListPosition/ns3:ConfigurationSummary, '##**##')"> 
        <xsl:value-of select="substring-after(ns3:ConfigurationSummaryListPosition/ns3:ConfigurationSummary,'##**##')"/> 
        </xsl:when> 
        <xsl:otherwise> 
        <xsl:value-of select="ns3:ConfigurationSummaryListPosition/ns3:ConfigurationSummary"/> 
        </xsl:otherwise> 
       </xsl:choose> 
       </xsl:if> 
      </xsl:if> 
      </xsl:for-each> 

     </xsl:variable> 

     <xsl:variable name="StatusVehicle"> 
      <xsl:for-each select="ns2:RouteParts/ns2:RoutePartListPosition/ns2:RoutePart/ns2:RoadPart/ns2:Vehicles"> 
      <xsl:if test="ns3:ConfigurationSummaryListPosition/ns3:ConfigurationSummary != ''"> 

       <xsl:choose> 
       <xsl:when test="contains(ns3:ConfigurationSummaryListPosition/ns3:ConfigurationSummary, '##**##')"> 

        <xsl:if test="$FirstVehicle != substring-after(ns3:ConfigurationSummaryListPosition/ns3:ConfigurationSummary,'##**##')"> 
        <xsl:value-of select="false()"/> 
        </xsl:if> 

       </xsl:when> 
       <xsl:otherwise> 
        <xsl:if test="$FirstVehicle != ns3:ConfigurationSummaryListPosition/ns3:ConfigurationSummary"> 
        <xsl:value-of select="false()"/> 
        </xsl:if> 
       </xsl:otherwise> 
       </xsl:choose> 

      </xsl:if> 
      </xsl:for-each> 
     </xsl:variable> 

     <xsl:variable name="CountVehicles"> 
      <xsl:for-each select="ns2:RouteParts/ns2:RoutePartListPosition/ns2:RoutePart"> 

      <xsl:if test="$UnitType=692001"> 
       <xsl:if test="$StatusVehicle = 'false'"> 
       <item> 
        Leg: 
        <xsl:choose> 
        <xsl:when test="contains(ns2:Name, '##**##')"> 
         <xsl:value-of select="substring-after(ns2:Name, '##**##')"/> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:value-of select="ns2:Name" /> 
        </xsl:otherwise> 
        </xsl:choose> 
        <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text> 
        <xsl:choose> 
        <!--Changes for RM#4998--> 
        <xsl:when test="contains(ns2:RoadPart/ns2:Distance/ns2:Metric/ns2:Distance, '##**##')"> 
         <xsl:call-template name="SplitAlternative"> 
         <xsl:with-param name="pText" select="substring-after(ns2:RoadPart/ns2:Distance/ns2:Metric/ns2:Distance, '##**##')"></xsl:with-param> 
         <xsl:with-param name="pDelim" select=" OR"></xsl:with-param> 
         </xsl:call-template> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:call-template name="SplitAlternative"> 
         <xsl:with-param name="pText" select="ns2:RoadPart/ns2:Distance/ns2:Metric/ns2:Distance"></xsl:with-param> 
         <xsl:with-param name="pDelim" select=" OR"></xsl:with-param> 
         </xsl:call-template> 
        </xsl:otherwise> 
        </xsl:choose> 
       </item> 
       </xsl:if> 
      </xsl:if> 

      <xsl:if test="$UnitType=692002"> 
       <xsl:if test="$StatusVehicle = 'false'"> 
       <item> 
        Leg: 
        <xsl:choose> 
        <xsl:when test="contains(ns2:Name, '##**##')"> 
         <xsl:value-of select="substring-after(ns2:Name, '##**##')"/> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:value-of select="ns2:Name" /> 
        </xsl:otherwise> 
        </xsl:choose> 
        <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text> 
        <xsl:choose> 
        <!--Changes for RM#4998--> 
        <xsl:when test="contains(ns2:RoadPart/ns2:Distance/ns2:Metric/ns2:Distance, '##**##')"> 
         <xsl:call-template name="SplitAlternative"> 
         <xsl:with-param name="pText" select="substring-after(ns2:RoadPart/ns2:Distance/ns2:Imperial/ns2:Distance, '##**##')"></xsl:with-param> 
         <xsl:with-param name="pDelim" select=" OR"></xsl:with-param> 
         </xsl:call-template> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:call-template name="SplitAlternative"> 
         <xsl:with-param name="pText" select="ns2:RoadPart/ns2:Distance/ns2:Imperial/ns2:Distance"></xsl:with-param> 
         <xsl:with-param name="pDelim" select=" OR"></xsl:with-param> 
         </xsl:call-template> 
        </xsl:otherwise> 
        </xsl:choose> 
       </item> 
       </xsl:if> 
      </xsl:if> 
      </xsl:for-each> 
     </xsl:variable> 

     <xsl:variable name="VehiclesArray" select="msxsl:node-set($CountVehicles)/item" /> 

     <!--Change Code End RM#4604 21 july--> 



     <!--For Semi Vehicles--> 
     <xsl:if test="ns2:RouteParts/ns2:RoutePartListPosition/ns2:RoutePart/ns2:RoadPart/ns2:Vehicles/ns3:VehicleSummaryListPosition/ns3:VehicleSummary/ns3:Configuration/ns3:SemiVehicle/ns3:Summary !=''"> 
      <xsl:for-each select="ns2:RouteParts/ns2:RoutePartListPosition/ns2:RoutePart/ns2:RoadPart/ns2:Vehicles/ns3:VehicleSummaryListPosition/ns3:VehicleSummary/ns3:Configuration/ns3:SemiVehicle"> 

      <xsl:variable name="getPosition" select="position()" /> 

      <xsl:if test="$getPosition &gt; 1"> 
       <b> 
       <xsl:value-of select="$VehiclesArray[$getPosition]"/> 
       </b> 
       <br></br> 
      </xsl:if> 
      <br/> 
      <table style ="margin-left" border = "1"> 
       <tr> 
       <td> 
        <b> 
        <!--TODO 1--> 
        Semi trailer 
        </b> 
       </td> 
       <td colspan="2"> 
        <b> 
        <xsl:if test="ns3:Summary!='' "> 
         <xsl:value-of select="ns3:Summary"/> 
        </xsl:if> 
        </b> 
       </td> 
       </tr> 
       <!--</xsl:if >--> 
       <xsl:if test="ns3:GrossWeight/ns3:Weight!=''"> 
       <tr> 
        <td>Gross weight:</td> 

        <td colspan="2"> 
        <xsl:if test="ns3:GrossWeight/ns3:Weight!=''"> 
         <xsl:choose> 
         <xsl:when test="contains(ns3:GrossWeight/ns3:Weight, '##**##')"> 
          <xsl:value-of select="substring-after(ns3:GrossWeight/ns3:Weight, '##**##')"/> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of select="ns3:GrossWeight/ns3:Weight"/> 
         </xsl:otherwise> 
         </xsl:choose> 

         <xsl:choose> 
         <xsl:when test="$UnitType='' or $UnitType=692001"> 
          <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>kg 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>kg 
         </xsl:otherwise> 
         </xsl:choose> 
        </xsl:if> 
        </td> 


       </tr> 
       </xsl:if> 
       <xsl:if test="ns3:AxleConfiguration/ns3:AxleWeightListPosition/ns3:AxleWeight !=''"> 
       <tr> 
        <td>Axle weight:</td> 

        <td colspan="2"> 
        <xsl:for-each select="ns3:AxleConfiguration/ns3:AxleWeightListPosition"> 

         <xsl:if test="ns3:AxleWeight!='' and ns3:AxleWeight/@AxleCount!=''"> 
         <xsl:choose> 
          <xsl:when test="contains(ns3:AxleWeight, '##**##')"> 
          <xsl:value-of select="substring-after(ns3:AxleWeight, '##**##')"/> 
          </xsl:when> 
          <xsl:otherwise> 
          <xsl:value-of select="ns3:AxleWeight"/> 
          </xsl:otherwise> 
         </xsl:choose> 
         <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>kg<xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text> x<xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text> 
         <xsl:choose> 
          <xsl:when test="contains(ns3:AxleWeight/@AxleCount, '##**##')"> 
          <xsl:value-of select="substring-after(ns3:AxleWeight/@AxleCount, '##**##')"/> 
          </xsl:when> 
          <xsl:otherwise> 
          <xsl:value-of select="ns3:AxleWeight/@AxleCount"/> 
          </xsl:otherwise> 
         </xsl:choose> 
         </xsl:if> 

         <xsl:if test="position() != last()"> 
         , 
         </xsl:if> 

        </xsl:for-each> 

        </td> 


       </tr> 
       </xsl:if> 
      </table> 
      <br/> 
      </xsl:for-each> 
     </xsl:if> 
     <!--For Semi Vehicles Ends here--> 

     <!--For Non Semi Vehicles--> 
     <xsl:if test="ns2:RouteParts/ns2:RoutePartListPosition/ns2:RoutePart/ns2:RoadPart/ns2:Vehicles/ns3:VehicleSummaryListPosition/ns3:VehicleSummary/ns3:Configuration/ns3:NonSemiVehicle/ns3:ComponentListPosition/ns3:Component/ns3:DrawbarTractor/ns3:Summary != '' 
        or ns2:RouteParts/ns2:RoutePartListPosition/ns2:RoutePart/ns2:RoadPart/ns2:Vehicles/ns3:VehicleSummaryListPosition/ns3:VehicleSummary/ns3:Configuration/ns3:NonSemiVehicle/ns3:ComponentListPosition/ns3:Component/ns3:LoadBearing/ns3:Summary != '' "> 

      <xsl:for-each select="ns2:RouteParts/ns2:RoutePartListPosition/ns2:RoutePart/ns2:RoadPart/ns2:Vehicles/ns3:VehicleSummaryListPosition/ns3:VehicleSummary/ns3:Configuration/ns3:NonSemiVehicle/ns3:ComponentListPosition"> 

      <xsl:variable name="getPosition" select="position()" /> 

      <xsl:if test="$getPosition &gt; 1"> 
       <b> 
       <xsl:value-of select="$VehiclesArray[$getPosition]"/> 
       </b> 
       <br></br> 
      </xsl:if> 

      <br></br> 
      <table style ="margin-left" border = "1"> 
       <tr> 
       <td> 
        <b> 
        <!--TODO 1--> 

        <xsl:choose> 
         <xsl:when test="contains(ns3:Component, 'tractor')"> 
         Tractor 
         </xsl:when> 
         <xsl:when test="contains(ns3:Component, 'trailer')"> 
         Trailer 
         </xsl:when> 
         <xsl:when test="contains(ns3:Component, 'spmt')"> 
         SPMT 
         </xsl:when> 
         <xsl:otherwise> 
         Tractor 
         </xsl:otherwise> 
        </xsl:choose> 
        </b> 
       </td> 
       <td colspan="2"> 
        <b> 
        <xsl:if test="contains(ns3:Component/ns3:DrawbarTractor/ns3:Summary, '##**##')=false()"> 
         <xsl:value-of select="ns3:Component/ns3:DrawbarTractor/ns3:Summary"/> 
        </xsl:if> 

        <xsl:if test="contains(ns3:Component/ns3:DrawbarTractor/ns3:Summary, '##**##')=true()"> 
         <xsl:value-of select="substring-after(ns3:Component/ns3:DrawbarTractor/ns3:Summary, '##**##')"/> 
        </xsl:if> 

        <xsl:if test="contains(ns3:Component/ns3:LoadBearing/ns3:Summary, '##**##')=false()"> 
         <xsl:value-of select="ns3:Component/ns3:LoadBearing/ns3:Summary"/> 
        </xsl:if> 

        <xsl:if test="contains(ns3:Component/ns3:LoadBearing/ns3:Summary, '##**##')=true()"> 
         <xsl:value-of select="substring-after(ns3:Component/ns3:LoadBearing/ns3:Summary, '##**##')"/> 
        </xsl:if> 
        </b> 
       </td> 
       </tr> 
       <!--</xsl:if >--> 
       <xsl:if test="ns3:Component/ns3:DrawbarTractor/ns3:Weight!=''"> 
       <tr> 
        <td>Gross weight:</td> 
        <td colspan="2"> 

        <xsl:call-template name="parseString"> 
         <xsl:with-param name="list" select="ns3:Component/ns3:DrawbarTractor/ns3:Weight"/> 
        </xsl:call-template> 

        <xsl:choose> 
         <xsl:when test="$UnitType='' or $UnitType=692001"> 
         <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>kg 
         </xsl:when> 
         <xsl:otherwise> 
         <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>kg 
         </xsl:otherwise> 
        </xsl:choose> 

        </td> 
       </tr> 
       </xsl:if> 
      </table> 
      <br/> 
      </xsl:for-each> 
     </xsl:if> 
     <!--For Non Semi Vehicles Ends here--> 

     </body> 
    </html> 
    </xsl:template> 

    <!--Changes for RM#4998 start--> 
    <xsl:template match="text()" name="SplitAlternative"> 
    <xsl:param name="pText" select="."/> 
    <xsl:param name="pDelim" select="' OR'"/> 
    <xsl:param name="pCounter" select="1"/> 
    <xsl:if test="string-length($pText) > 0"> 
     <xsl:variable name="vToken" select= 
    "substring-before(concat($pText,' OR'), ' OR')"/> 
     <!--<xsl:value-of select="$vToken"/> 
     <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;,&nbsp;]]></xsl:text> 
     <xsl:value-of select="$pCounter"/>--> 

     <xsl:if test="not($pCounter = 1)"> 
     or<xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text> 
     </xsl:if> 
     <xsl:if test="$UnitType='' or $UnitType=692001"> 
     <xsl:variable name="varKM" select="round(number($vToken) div number(1000))"/> 
     <xsl:value-of select="$varKM"/> <xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>km 
     </xsl:if> 
     <xsl:if test="$UnitType=692002"> 
     <xsl:variable name="varMiles" select="round(number($vToken) div number(1760))"/> 
     <xsl:value-of select="$varMiles"/><xsl:text disable-output-escaping="yes"><![CDATA[&nbsp;]]></xsl:text>miles 
     </xsl:if> 
     <xsl:call-template name="SplitAlternative"> 
     <xsl:with-param name="pText" select= 
     "substring-after($pText,'OR')"/> 
     <xsl:with-param name="pCounter" 
     select="$pCounter + 1"/> 
     </xsl:call-template> 

    </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 
+1

请为每个输入XML文档显示一个完整的(和最小的)XSLT样式表,并清楚地显示输出应该是什么。谢谢。更多帮助:http://stackoverflow.com/help/mcve。 –

+0

XSLT非常大,输出对此没有任何影响,我只想要一个IF条件的动态顺序。让我知道是否需要更多的澄清。非常感谢。 –

+0

我_already_让你知道需要更多的说明。问题在于你的XSLT样式表非常大。将其分解并在一个非常小的样式表中隔离您的问题并将其发布到此处。 –

回答

0

为什么需要那些条件检查?如果单纯使用

<xsl:template match="/"> 
    <xsl:apply-templates select="ns2:RoadPart/ns2:Vehicles/*"/> 
</xsl:template> 

处理的Vehicles所有子元素或者,如果有一些你不想来处理你当然也可以使用

<xsl:template match="/"> 
    <xsl:apply-templates select="ns2:RoadPart/ns2:Vehicles/ns3:SemiVehicle | ns2:RoadPart/ns2:Vehicles/ns3:NonsemiVehicle"/> 
</xsl:template> 

加模板(或更多)用于输出要输出的值,然后处理元素并按文档顺序输出。

0

你的条件

ns2:RoadPart/ns2:Vehicles/ns3:SemiVehicle 

如果发现至少一个SemiVehicle,条件

ns2:RoadPart/ns2:Vehicles/ns3:NonsemiVehicle 

是真实的,如果有发现至少有一个NonsemiVehicle是真实的。

所以通常这两个条件都是真的,两个分支都会被执行。我怀疑这不是你想要的。我怀疑你想要处理所有的车辆,对于SemiVehicles和NonsemiVehicles使用不同的逻辑。

这就是模板规则的用途。我觉得你的代码应该是这个样子:

<xsl:apply-templates select="ns2:RoadPart/ns2:Vehicles/*"/> 

然后

<xsl:template match="ns3:SemiVehicle"> 
    //code to show values from XML(ie 20,20) 
</xsl:template> 

<xsl:template match="ns3:NonsemiVehicle"> 
    //code to show values from XML(ie 10,10) 
</xsl:template> 

除了为这两种车辆的结构似乎是如此的相似,你可能要使用同一个模板规则都。