根据子值删除父级?

时间:2014-09-22 15:59:18

标签: xslt

我想只显示那些没有OrderLineSource = YTR的订单。应该显示所有其他....

我的示例XML:

<Orders>
<Order>
  <OrderID>34209649</OrderID>
  <OrderStatus>checkout_complete</OrderStatus>
  <Amount>32.93</Amount>
  <OrderCreation>2014-02-08T00:00:03.00</OrderCreation>
  <OrderCompletion>2014-02-08T00:00:03.00</OrderCompletion>
  <CustomerGUID>303965683</CustomerGUID>
  <CSMPurchaserGUID>0</CSMPurchaserGUID>
  <Brand>TRFE</Brand>
  <SourceECommerceSystem>Framework</SourceECommerceSystem>
  <Currency>GBP</Currency>
  <OrderChannel>Online</OrderChannel>
  <TransactionSummary>
     <TransactionID>2407065</TransactionID>
     <MerchantReference>TEdV-5648-34209649</MerchantReference>
     <CardCategory>Personal</CardCategory>
     <CardScheme>VISA Debit</CardScheme>
     <CardCountry>gbr</CardCountry>
     <CardIssuer>sdfsdf sdf Bank asdf</CardIssuer>
     <CardStartDate>0/0</CardStartDate>
     <CardExpiryDate>2016/08</CardExpiryDate>
     <Amount>32.93</Amount>
     <Currency>GBP</Currency>
     <CardPAN>************4585</CardPAN>
     <Created>2014-02-07T23:56:48</Created>
     <Updated>2014-02-08T00:00:03</Updated>
     <ResponseStatusCode>1</ResponseStatusCode>
     <ResponseStatusReason>FULFILLED OK</ResponseStatusReason>
     <HostedPageIdentifier>dsfasdf-ee85-4afa-bb6a-0afc6dc99896</HostedPageIdentifier>
     <HostedPageURL>https://hps.datacash.com/hps/</HostedPageURL>
     <PaymentStatus>Paid</PaymentStatus>
     <PaymentType>Debit Card</PaymentType>
     <NameOnCard>Miss L J adsf</NameOnCard>
     <DataCashRef>56456456454</DataCashRef>
     <MerchantID>545646</MerchantID>
     <ThreeDCard>1</ThreeDCard>
     <ThreeDRequested>1</ThreeDRequested>
     <IPAddress>127.89.560.1</IPAddress>
  </TransactionSummary>
  <OrderLine>
     <OrderLineID>84598837</OrderLineID>
     <OrderID>34209649</OrderID>
     <OrderLineLabel>GAREGSBV</OrderLineLabel>
     <OrderLineSource>GHR</OrderLineSource>
     <Quantity>1</Quantity>
     <UnitPrice>32.93</UnitPrice>
     <Total>32.93</Total>
     <SKUCode>P0032</SKUCode>
     <Title>Miss.</Title>
     <FirstName>ertwer</FirstName>
     <FamilyName>sdaf</FamilyName>
     <DateOfBirth>1984-05-30</DateOfBirth>
     <Email>sdfasdfa@hotmail.com</Email>
     <Mobile>645646454</Mobile>
     <PostChannel>0</PostChannel>
     <TelephoneChannel>0</TelephoneChannel>
     <EmailChannel>0</EmailChannel>
     <TextAndOtherChannel>0</TextAndOtherChannel>
     <BuildingNumber>27</BuildingNumber>
     <AddressLine1>27</AddressLine1>
     <AddressLine2>dsfasdf Road</AddressLine2>
     <Town>London</Town>
     <Country>sdfasdf er</Country>
     <Postcode>KL7 2NS</Postcode>
     <AddressValidated>1</AddressValidated>
     <HKPolicy>
        <PolicyNum>PP01754397</PolicyNum>
        <ProductDescription>sadfsadfasdfgasdg</ProductDescription>
        <CoverTypeDesc>Individual</CoverTypeDesc>
        <SingleParentFamilyFlag>0</SingleParentFamilyFlag>
        <PolicyTypeRefID>S</PolicyTypeRefID>
        <PolicyTypeDesc>Sinasdfnce</PolicyTypeDesc>
        <TierDesc>Classic</TierDesc>
        <DestinationDesc>Worldwide including USA, Canada, Caribbean</DestinationDesc>
        <TotalTravellers>1</TotalTravellers>
        <NumOfAdults>1</NumOfAdults>
        <NumOfUnder18>0</NumOfUnder18>
        <PolicyStartDate>2014-02-08</PolicyStartDate>
        <PolicyEndDate>2014-02-12</PolicyEndDate>
        <BaseCost>32.93</BaseCost>
        <Commission>11.18</Commission>
        <UpsoldInd>0</UpsoldInd>
        <TierRefID>C</TierRefID>
        <DestinationRefID>W2</DestinationRefID>
        <CoverTypeRefID>I</CoverTypeRefID>
        <AONToPostPolicy>yes</AONToPostPolicy>
        <SalesChannel>0011002</SalesChannel>
        <WhereYouHeardOfUs>Press advertising</WhereYouHeardOfUs>
        <TIPOLTraveller>
           <TravellerUUID>1864-1</TravellerUUID>
           <PolicyNum>PI0e31754397</PolicyNum>
           <Title>Miss</Title>
           <FirstName>sdfsf</FirstName>
           <FamilyName>sdfsdf</FamilyName>
           <DateOfBirth>1984-05-30</DateOfBirth>
           <AgeBand>1864</AgeBand>
           <DependentFlag>0</DependentFlag>
        </TIPOLTraveller>
     </TIPOLPolicy>
  </OrderLine>
  <OrderCustomerDetails>
     <Title nil="true" />
     <FirstName nil="true" />
     <SecondName nil="true" />
     <FamilyName nil="true" />
     <DateOfBirth nil="true" />
     <Email nil="true" />
     <Telephone nil="true" />
     <Mobile nil="true" />
     <Gender nil="true" />
     <PostChannel nil="true" />
     <TelephoneChannel nil="true" />
     <EmailChannel nil="true" />
     <TextAndOtherChannel nil="true" />
     <BuildingNumber>27</BuildingNumber>
     <AddressLine1>27</AddressLine1>
     <AddressLine2>asdfa Road</AddressLine2>
     <Town>asdfasdf</Town>
     <Country>United dsf</Country>
     <Postcode>KH9 2NS</Postcode>
     <AddressValidated>1</AddressValidated>
  </OrderCustomerDetails>
</Order>
<Order>
  <OrderID>34209674</OrderID>
  <OrderStatus>checkout_complete</OrderStatus>
  <Amount>11.13</Amount>
  <OrderCreation>2014-02-08T00:08:40.00</OrderCreation>
  <OrderCompletion>2014-02-08T00:08:40.00</OrderCompletion>
  <CustomerGUID>303965688</CustomerGUID>
  <CSMPurchaserGUID>0</CSMPurchaserGUID>
  <Brand>TRFDS</Brand>
  <SourceECommerceSystem>Framework</SourceECommerceSystem>
  <Currency>GBP</Currency>
  <OrderChannel>Online</OrderChannel>
  <TransactionSummary>
     <TransactionID>8115032</TransactionID>
     <MerchantReference>JHF-0800-34209674</MerchantReference>
     <CardCategory>Personal</CardCategory>
     <CardScheme>VISA Debit</CardScheme>
     <CardCountry>gbr</CardCountry>
     <CardIssuer>Unknown</CardIssuer>
     <CardStartDate>0/0</CardStartDate>
     <CardExpiryDate>2016/09</CardExpiryDate>
     <Amount>11.13</Amount>
     <Currency>GBP</Currency>
     <CardPAN>************4849</CardPAN>
     <Created>2014-02-08T00:08:00</Created>
     <Updated>2014-02-08T00:08:40</Updated>
     <ResponseStatusCode>1</ResponseStatusCode>
     <ResponseStatusReason>FULFILLED OK</ResponseStatusReason>
     <HostedPageIdentifier>f3306487-d6ea-4200-9eea-99b1d6832a2e</HostedPageIdentifier>
     <HostedPageURL>https://hps.dat.com/hps/</HostedPageURL>
     <PaymentStatus>Paid</PaymentStatus>
     <PaymentType>Debit Card</PaymentType>
     <NameOnCard>Miss Jor </NameOnCard>
     <DataCashRef>380010093738013</DataCashRef>
     <MerchantID>21877049</MerchantID>
     <ThreeDCard>1</ThreeDCard>
     <ThreeDRequested>1</ThreeDRequested>
     <IPAddress>86..25640.99</IPAddress>
  </TransactionSummary>
  <OrderLine>
     <OrderLineID>84598874</OrderLineID>
     <OrderID>34209674</OrderID>
     <OrderLineLabel>3-1008617753325</OrderLineLabel>
     <OrderLineSource>YTR</OrderLineSource>
     <Quantity>1</Quantity>
     <UnitPrice>11.13</UnitPrice>
     <Total>11.13</Total>
     <Title>Miss.</Title>
     <FirstName>Jordan</FirstName>
     <SecondName>oirut</SecondName>
     <FamilyName>dfgsdfgs</FamilyName>
     <Email>dfgsdfg@hotmail.com</Email>
     <Mobile>654756464</Mobile>
     <PostChannel>0</PostChannel>
     <TelephoneChannel>0</TelephoneChannel>
     <EmailChannel>0</EmailChannel>
     <TextAndOtherChannel>0</TextAndOtherChannel>
     <BuildingNumber>12</BuildingNumber>
     <AddressLine1>12</AddressLine1>
     <AddressLine2>sfgsdfg End Gardens</AddressLine2>
     <Town>HEMEL sfgaefa</Town>
     <Country>adf dgfsdfg</Country>
     <Postcode>HP1 1SN</Postcode>
     <OrderLineDetail>
        <NameValuePair>
           <Name>dfgsdfg</Name>
           <Value>628</Value>
        </NameValuePair>
        <NameValuePair>
           <Name>NameOnCard</Name>
           <Value>adsfgasdgf Piper</Value>
        </NameValuePair>
        <NameValuePair>
           <Name>DateOnCard</Name>
           <Value>2014-02-05</Value>
        </NameValuePair>
        <NameValuePair>
           <Name>CustomsOrSurcharge</Name>
           <Value>CUSTOMS CHARGE TO PAY</Value>
        </NameValuePair>
     </OrderLineDetail>
  </OrderLine>
  <OrderCustomerDetails>
     <Title>Miss.</Title>
     <FirstName>Jordan</FirstName>
     <SecondName>asdgfasdgf</SecondName>
     <FamilyName nil="true" />
     <DateOfBirth />
     <Email>adfadf@hotmail.com</Email>
     <Telephone />
     <Mobile>adfasdf</Mobile>
     <Gender nil="true" />
     <PostChannel nil="true" />
     <TelephoneChannel nil="true" />
     <EmailChannel nil="true" />
     <TextAndOtherChannel nil="true" />
     <BuildingNumber>12</BuildingNumber>
     <AddressLine1>12</AddressLine1>
     <AddressLine2>adfasdf End Gardens</AddressLine2>
     <Town>adsfasdf HEMPSTEAD</Town>
     <Country>United asdfasdf</Country>
     <Postcode>asd 1SN</Postcode>
  </OrderCustomerDetails>
</Order>
</Orders>

我尝试使用XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Orders -->
<xsl:template match="/*">
 <xsl:element name="Orders">
   <xsl:apply-templates select="./Order" /> 
 </xsl:element>
</xsl:template>
<!-- Orders > Order -->
<xsl:template match="/Order">
  <xsl:variable name="IsValid">
  <xsl:call-template name="HasOrIsValidPOLine" />
</xsl:variable>
 <xsl:if test="$IsValid='VALID'"> <!-- only display the order if there's a valid line under it-->
  <xsl:element name="Order">
    <xsl:apply-templates select=".//VORNR" />
  </xsl:element>
</xsl:if>
</xsl:template>
 <!-- Part Order List > Part Order > Operational BO Number -->
 <xsl:template match="//VORNR">
 <xsl:element name="./Order">
 <xsl:apply-templates select="node()|@*"/>
  <xsl:value-of select="text()"/>
  </xsl:element>
</xsl:template>
<xsl:template name="HasOrIsValidPOLine">
 <xsl:choose>
   <xsl:when test="./OrderLineSource/text() != 'YTR'">VALID</xsl:when>
   <xsl:otherwise>INVALID</xsl:otherwise>
 </xsl:choose>
</xsl:template>
<xsl:template match="node()|@*">
 <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>

您可以向我提供解决方案或告诉我出错的地方

2 个答案:

答案 0 :(得分:1)

首先,您的示例XML格式不正确:它包含一个与起始</TIPOLPolicy>标记不匹配的结束<HKPolicy>标记。首先将其更改为</HKPolicy>

之后,以下XSLT 1.0可以满足您的需求:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <!-- Identity transform -->
    <!-- Default priority 0 for root node and -0.5 for the rest -->
    <xsl:template match="/ | node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <!-- Do nothing for Order elements whose OrderLine/OrderLineSource equals 'YTR' -->
    <!-- Default priority 0.5 -->
    <xsl:template match="Order[OrderLine/OrderLineSource = 'YTR']"/>
</xsl:stylesheet>

它使用身份转换和不同的默认优先级:具有较低默认优先级的身份转换将输入复制到输出,除非给定输入匹配存在具有更高优先级的另一个模板。对于OrderLine/OrderLineSource后代包含文本值&#39; YTR&#39;的订单元素,就是这种情况。由于其较高的默认优先级,更具体的模板优先于身份转换。由于模板不会产生任何输出,因此会从输出中删除任何匹配的Order元素。

答案 1 :(得分:0)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="Order[OrderLine/OrderLineSource[string() = 'YTR']]"/>

</xsl:stylesheet>