我在XSLT中有两个if条件,如下所示:
<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>
每当我首次列出SemiVehicle并将非赛车列为第二时,我解析此XSLT。我每次都根据我给定的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[ ]]></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[ ]]></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 > 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[ ]]></xsl:text>kg
</xsl:when>
<xsl:otherwise>
<xsl:text disable-output-escaping="yes"><![CDATA[ ]]></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[ ]]></xsl:text>kg<xsl:text disable-output-escaping="yes"><![CDATA[ ]]></xsl:text> x<xsl:text disable-output-escaping="yes"><![CDATA[ ]]></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 > 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[ ]]></xsl:text>kg
</xsl:when>
<xsl:otherwise>
<xsl:text disable-output-escaping="yes"><![CDATA[ ]]></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[ , ]]></xsl:text>
<xsl:value-of select="$pCounter"/>-->
<xsl:if test="not($pCounter = 1)">
or<xsl:text disable-output-escaping="yes"><![CDATA[ ]]></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[ ]]></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[ ]]></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>
答案 0 :(得分: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>
加上一个模板(或更多),用于输出您想要输出的值,然后按文档顺序处理和输出元素。
答案 1 :(得分:0)
你的病情
Stream<String> names = Arrays.asList("NAME A", "NAME B", "NAME A").stream();
final Stream<String> distinct = names.distinct();
System.out.println(distinct.collect(Collectors.joining(",")));
如果找到至少一个SemiVehicle,条件,则为真
ns2:RoadPart/ns2:Vehicles/ns3:SemiVehicle
如果找到至少一个NonsemiVehicle,则为真。
因此通常两个条件都是正确的,并且两个分支都将被执行。我怀疑这不是你想要的。我怀疑你想要处理所有的车辆,对于SemiVehicles和NonsemiVehicles有不同的逻辑。
那是什么模板规则。我认为你的代码应该是这样的:
ns2:RoadPart/ns2:Vehicles/ns3:NonsemiVehicle
然后
<xsl:apply-templates select="ns2:RoadPart/ns2:Vehicles/*"/>
除了两种车辆的结构看起来如此相似以至于您可能希望对两者使用相同的模板规则。