XSLT remove child element based on parent-child element's value

后端 未结 1 1591
深忆病人
深忆病人 2021-01-23 15:52

I am trying to use the some what same logic that is used in here https://stackoverflow.com/a/10629806/662877

to a different XML but not getting the intended output.

相关标签:
1条回答
  • 2021-01-23 16:39

    The problem is here:

    *[../TERR='KHM6']/CreatePaymentItems
    

    This matches the CreatePaymentItems child of an element that has a sibling TERR with value "KHM6".

    But all siblings of TERR in the provided XML document are child-less.

    Here is the transformation you want:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
     "*[PayLineItems/*/TERR = 'KHM6']
          /*[self::CreatePaymentItems or self::CreatePayExt]"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document (corrected to be well-formed):

    <ns0:DocTran doctype="Return" xmlns:ns0="http://test.Schemas.Out_Return">
        <TransactionType>
            <PayLineItems>
                <PayLineInsert>
                    <PAYTYPE>4</PAYTYPE>
                    <NUMBER>R2293416</NUMBER>
                    <SERIAL>3335889530</SERIAL>
                    <DOCDATE>05/03/2012</DOCDATE>
                    <ITEMNMBR>4NZ3330000010</ITEMNMBR>
                    <UNITPRCE>599.99</UNITPRCE>
                    <XTNDPRCE>599.99</XTNDPRCE>
                    <QUANTITY>1</QUANTITY>
                    <TAXAMNT>43.65</TAXAMNT>
                    <QTYONHND>1</QTYONHND>
                    <TERR>KHM6</TERR>
                </PayLineInsert>
            </PayLineItems>
            <PayLineEXT>
                <PAYTYPE>4</PAYTYPE>
                <NUMBER>R2293416</NUMBER>
                <LNITMSEQ>2293416</LNITMSEQ>
            </PayLineEXT>
            <CreatePaymentItems>
                <CreatePaymentInsertRecord>
                    <INTERID>TOTM</INTERID>
                    <PAYTYPE>4</PAYTYPE>
                    <PAYNUMBER>R2293416</PAYNUMBER>
                    <DOCDATE>05/03/2012</DOCDATE>
                    <DOCAMOUNT>645.03</DOCAMOUNT>
                    <DOCNUMBER>2293416</DOCNUMBER>
                </CreatePaymentInsertRecord>
            </CreatePaymentItems>
            <CreatePayExt>
                <PAYTYPE>4</PAYTYPE>
                <PAYNUMBER>R2293416</PAYNUMBER>
                <SEQNUMBR>61261585</SEQNUMBR>
            </CreatePayExt>
        </TransactionType>
    </ns0:DocTran>
    

    the wanted, correct result is produced:

    <ns0:DocTran xmlns:ns0="http://test.Schemas.Out_Return" doctype="Return">
       <TransactionType>
          <PayLineItems>
             <PayLineInsert>
                <PAYTYPE>4</PAYTYPE>
                <NUMBER>R2293416</NUMBER>
                <SERIAL>3335889530</SERIAL>
                <DOCDATE>05/03/2012</DOCDATE>
                <ITEMNMBR>4NZ3330000010</ITEMNMBR>
                <UNITPRCE>599.99</UNITPRCE>
                <XTNDPRCE>599.99</XTNDPRCE>
                <QUANTITY>1</QUANTITY>
                <TAXAMNT>43.65</TAXAMNT>
                <QTYONHND>1</QTYONHND>
                <TERR>KHM6</TERR>
             </PayLineInsert>
          </PayLineItems>
          <PayLineEXT>
             <PAYTYPE>4</PAYTYPE>
             <NUMBER>R2293416</NUMBER>
             <LNITMSEQ>2293416</LNITMSEQ>
          </PayLineEXT>
       </TransactionType>
    </ns0:DocTran>
    
    0 讨论(0)
提交回复
热议问题