如何从Charge中获取车辆详细信息,否则从Bond获取?

时间:2016-02-17 18:12:59

标签: xml xslt

我想从Case / Charge获取车辆信息(如果存在)。如果没有,那么我想从Bond / Charge获得它。 我该怎么做?

我修改了调用Charge模板的代码,如下所示

<xsl:for-each select="Charge[($pCallType='Deleted') or ((Deleted!='true') or (string-length(Deleted)=0))]/ChargeHistory[($pCallType='Deleted') or ((Deleted!='true') or (string-length(Deleted)=0))][@Stage='Bond Posted'][last()]">
<xsl:if test="count(//Case/Charge/ChargeHistory[@ChargeHistoryID=current()/@ChargeHistoryID])">
<xsl:for-each select="//Case/Charge/ChargeHistory[@ChargeHistoryID=current()/@ChargeHistoryID]">
<xsl:choose>
    <xsl:when test="//Case/Charge/ChargeHistory[@ChargeHistoryID=current()/@ChargeHistoryID]">
        <xsl:call-template name="Charge"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:call-template name="Charge"/>
    </xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:if>

以下是我想要的当前输出唯一的问题是,当没有Case是Bond的父级时,此代码不起作用。

            <Vehicle>
            <VehicleTypeText code="PASSVEH">Passenger Vehicle</VehicleTypeText>
            <VehicleMakeText code="RAM">Ram Trucks</VehicleMakeText>
            <VehicleModelText>Undetermined</VehicleModelText>
            <VehicleYear>2015</VehicleYear>
            <VehicleColorText code="BRO">Brown</VehicleColorText>
            <VehicleLicensePlate>
                <PlateState>WI</PlateState>
                <PlateNumber>
                    <ID>4896LL</ID>
                </PlateNumber>
            </VehicleLicensePlate>
            <VehicleID>
        </VehicleID>

以下是使用上述xsl代码的xml文档

    <SourceXML>
    <Integration>
        <Case>
            <Charge>
                <Vehicle>
                    <VehicleLicensePlateState>WI</VehicleLicensePlateState>
                    <VehicleLicensePlateNumber>4896LL</VehicleLicensePlateNumber>
                    <VehicleType Word="PASSVEH">Passenger Vehicle</VehicleType>
                    <VehicleYear>2015</VehicleYear>
                    <VehicleMake Word="RAM">Ram Trucks</VehicleMake>
                    <VehicleModel>Undetermined</VehicleModel>
                    <CommercialVehicleFlag>false</CommercialVehicleFlag>
                    <HazardousVehicleFlag>false</HazardousVehicleFlag>
                </Vehicle>
                <ChargeHistory ChargeHistoryID="42961096" Stage="Bond Posted">
                    <Deleted>false</Deleted>
                </ChargeHistory>
            </Charge>
        </Case>
        <Bond Op="E" InternalID="1610459981">
            <Charge ID="10918642">
                <ChargeOffenseDate>12/08/2015</ChargeOffenseDate>
                <Vehicle>
                    <VehicleLicensePlateState>WI</VehicleLicensePlateState>
                    <VehicleLicensePlateNumber>4896LL</VehicleLicensePlateNumber>
                </Vehicle>
                <ChargeHistory ChargeHistoryID="42961096" Stage="Bond Posted">
                    <Deleted>false</Deleted>
                </ChargeHistory>
            </Charge>
        </Bond>
    </Integration>
</SourceXML>

以下xml文档无法使用上面的xslt代码。车辆信息没有输出

<SourceXML>
<Integration>
    <Bond>
        <Charge ID="10918642" PartyID="16766539" InternalChargeID="1616725033" InternalPartyID="1614668634" xmlns:reslib="urn:reslib">
            <Vehicle>
                <VehicleLicensePlateState>WI</VehicleLicensePlateState>
                <VehicleLicensePlateNumber>4896LL</VehicleLicensePlateNumber>
            </Vehicle>
                <ChargeHistory ChargeHistoryID="42960888" Stage="Case Filing" FilingSequence="1" InternalOffenseHistoryID="1637054698">
                <Deleted>false</Deleted>
                </ChargeHistory>
                <ChargeHistory ChargeHistoryID="42961120" Stage="Case Filing" FilingSequence="2" InternalOffenseHistoryID="1637054930">


                    <Deleted>false</Deleted>
                </ChargeHistory>
                <ChargeHistory ChargeHistoryID="42961096" Stage="Bond Posted" BondEventSequence="1" InternalOffenseHistoryID="1637054906">
            <Deleted>false</Deleted>
                </ChargeHistory>
            </Charge>
        </Bond>
</Integration>
</SourceXML>

1 个答案:

答案 0 :(得分:0)

考虑有条件地应用Case vs Bond模板:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>

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

  <xsl:template match="Integration">    
      <xsl:choose>
         <!-- CONDITIONALLY APPLY BASE TEMPLATE IF CASE IS EMPTY -->
         <xsl:when test="string-length(Case/Charge/Vehicle) &gt; 0">     
            <xsl:apply-templates select="Case/Charge/Vehicle"/>
         </xsl:when>
         <xsl:otherwise>              
            <xsl:apply-templates select="Bond/Charge/Vehicle"/>
         </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template match="Vehicle">
    <VehicleID>      
      <VehicleTypeText><xsl:value-of select="VehicleType"/></VehicleTypeText>
      <VehicleMakeText><xsl:value-of select="VehicleMake"/></VehicleMakeText>
      <VehicleModelText><xsl:value-of select="VehicleModel"/></VehicleModelText>
      <VehicleYear><xsl:value-of select="VehicleYear"/></VehicleYear>
      <VehicleColorText><xsl:value-of select="VehicleColor"/></VehicleColorText>
      <VehicleLicensePlate>
        <PlateState>        
            <xsl:value-of select="VehicleLicensePlateState"/>                                 
        </PlateState>
        <PlateNumber>
            <ID><xsl:value-of select="VehicleLicensePlateNumber"/></ID>
        </PlateNumber>        
      </VehicleLicensePlate>
    </VehicleID>
  </xsl:template>       
</xsl:transform>