簡體   English   中英

從python中的ElementTree中刪除空白的lxml元素

[英]Removing blank lxml element from ElementTree in python

我已經為此苦苦掙扎了幾天,我想我會在這里問。

我正在准備 XML 有效負載以 POST 到包含財務數據的 Oracle 端點。 我已經按照 Oracle 規范構建了大部分 XML,但是我在其中的一個方面苦苦掙扎。 這是將提供給總賬財務系統的數據,xml 結構如下(為了減少帖子,省略了一些元素。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://xmlns.oracle.com/apps/financials/generalLedger/journals/desktopEntry/journalImportService/types/" xmlns:jour="http://xmlns.oracle.com/apps/financials/generalLedger/journals/desktopEntry/journalImportService/">
  <soapenv:Header/>
  <soapenv:Body>
    <typ:importJournals>
      <typ:interfaceRows>
        <jour:BatchName>batch</jour:BatchName>
        <jour:AccountingPeriodName>Aug-20</jour:AccountingPeriodName>
        <jour:AccountingDate>2020-08-31</jour:AccountingDate>
        <jour:GlInterface>
          <jour:LedgerId>1234567890</jour:LedgerId>
          <jour:PeriodName>Aug-20</jour:PeriodName>
          <jour:AccountingDate>2020-08-31</jour:AccountingDate>
          <jour:Segment1>1</jour:Segment1>
          <jour:Segment2>1</jour:Segment2>
          <jour:Segment3>1</jour:Segment3>
          <jour:Segment4>1</jour:Segment4>
          <jour:Segment5>0</jour:Segment5>
          <jour:Segment6>0</jour:Segment6>
          <jour:CurrencyCode>USD</jour:CurrencyCode>
          <jour:EnteredCrAmount currencyCode="USD">10.0000</jour:EnteredCrAmount>
        </jour:GlInterface>
        <jour:GlInterface>
          <jour:LedgerId>1234567890</jour:LedgerId>
          <jour:PeriodName>Aug-20</jour:PeriodName>
          <jour:AccountingDate>2020-08-31</jour:AccountingDate>
          <jour:Segment1>2</jour:Segment1>
          <jour:Segment2>2</jour:Segment2>
          <jour:Segment3>2</jour:Segment3>
          <jour:Segment4>2</jour:Segment4>
          <jour:Segment5>0</jour:Segment5>
          <jour:Segment6>0</jour:Segment6>
          <jour:CurrencyCode>USD</jour:CurrencyCode>
          <jour:EnteredDrAmount currencyCode="USD">10.0000</jour:EnteredCrAmount>
        </jour:GlInterface>
      </typ:interfaceRows>
    </typ:importJournals>
  </soapenv:Body>
</soapenv:Envelope>

因此,如果您查看上面的 XML,在 GlInterface 標記中,每筆交易有 2 個(一個是借方,一個是貸方,如果您查看 Segments(帳戶代碼),它們是不同的,一個 GlInterface 標記作為EnteredDrAmount 標簽,而另一個有 EnteredCrAmount 標簽。

在源數據中,Cr 或 Dr 標記為空,具體取決於行是借方還是貸方,在 python 中顯示為“無”。

我讓它工作的方法是調用兩個調用來獲取數據,一個是 Cr 不為空,一個是 Dr 不為空,這個過程工作正常,但在 Python 中,我得到一個錯誤“只有一個 * 允許” . 代碼如下。

    xmlOracle = x_Envelope(
        x_Header,
        x_Body(
            x_importJournals(
                x_interfaceRows(
                    x_h_BatchName(str(batch[0])),
                    x_h_AccountingPeriodName(str(batch[3])),
                    x_h_AccountingDate(str(batch[4])),
                    *[x_GlInterface(
                        x_d_LedgerId(str(adid[0])),
                        x_d_PeriodName(str(adid[1])),
                        x_d_AccountingDate(str(adid[2])),
                        x_d_Segment1(str(adid[5])),
                        x_d_Segment2(str(adid[6])),
                        x_d_Segment3(str(adid[7])),
                        x_d_Segment4(str(adid[8])),
                        x_d_Segment5(str(adid[9])),
                        x_d_Segment6(str(adid[10])),
                        x_d_CurrencyCode(str(adid[11])),
                        x_d_EnteredCrAmount(str(adid[14]), currencyCode=str(adid[11]))
                    ) for adid in CrAdidToProcess],
                    *[x_GlInterface(
                        x_d_LedgerId(str(adid[0])),
                        x_d_PeriodName(str(adid[1])),
                        x_d_AccountingDate(str(adid[2])),
                        x_d_Segment1(str(adid[5])),
                        x_d_Segment2(str(adid[6])),
                        x_d_Segment3(str(adid[7])),
                        x_d_Segment4(str(adid[8])),
                        x_d_Segment5(str(adid[9])),
                        x_d_Segment6(str(adid[10])),
                        x_d_CurrencyCode(str(adid[11])),
                        x_d_EnteredDrAmount(str(adid[14]), currencyCode=str(adid[11]))
                    ) for adid in DrAdidToProcess]
                )
            )
        )
    )

我也試過打一個電話來獲取線路詳細信息,然后刪除或過濾掉標簽(Cr 或 Dr),如果它是“無”,但我對此並不走運。

雖然上述過程有效,但我的代碼中存在錯誤,我希望我的代碼中沒有錯誤。

謝謝你們。

經過進一步測試,我相信我找到了解決方案。 我相信我試圖從 ElementTree 對象中刪除一個元素,但它沒有任何元素。 當我將一個元素傳遞給 remove 方法/函數時,它終於起作用了。

這是刪除“無”條目的函數的代碼。

def removeCrDrEmptyElements(element):
    for removeElement in element.xpath('/soapenv:Envelope/soapenv:Body/typ:importJournals/typ:interfaceRows/jour:GlInterface/jour:EnteredCrAmount',
                                         namespaces = { 'soapenv' : 'http://schemas.xmlsoap.org/soap/envelope/',
                                                        'typ' : 'http://xmlns.oracle.com/apps/financials/generalLedger/journals/desktopEntry/journalImportService/types/',
                                                        'jour' : 'http://xmlns.oracle.com/apps/financials/generalLedger/journals/desktopEntry/journalImportService/'
                                                      }):
        if removeElement.text == 'None':
            removeElement.getparent().remove(removeElement)

    for removeElement in element.xpath('/soapenv:Envelope/soapenv:Body/typ:importJournals/typ:interfaceRows/jour:GlInterface/jour:EnteredDrAmount',
                                         namespaces = { 'soapenv' : 'http://schemas.xmlsoap.org/soap/envelope/',
                                                        'typ' : 'http://xmlns.oracle.com/apps/financials/generalLedger/journals/desktopEntry/journalImportService/types/',
                                                        'jour' : 'http://xmlns.oracle.com/apps/financials/generalLedger/journals/desktopEntry/journalImportService/'
                                                      }):
        if removeElement.text == 'None':
            removeElement.getparent().remove(removeElement)

    return element

顯然,這可以更好地重寫(我會這樣做),但我只想檢查 GlInterface 標記中的兩個元素,即 EnteredCrAmount 和 EnteredDrAmount,如果文本為 None,則刪除這些元素。

然后,您可以使用以下代碼調用該函數,以返回帶有已刪除 Nulls/Nones 的元素類型

xmlWithoutNull = removeCrDrEmptyElements(xmlElement)

運行函數結果前的輸出:

        <jour:GlInterface>
          # omitted elements
          <jour:EnteredCrAmount currencyCode="USD">1.000000</jour:EnteredCrAmount>
          <jour:EnteredDrAmount currencyCode="USD">None</jour:EnteredDrAmount>
          # omitted elements
        </jour:GlInterface>
        <jour:GlInterface>
          # omitted elements
          <jour:EnteredCrAmount currencyCode="USD">None</jour:EnteredCrAmount>
          <jour:EnteredDrAmount currencyCode="USD">1.000000</jour:EnteredDrAmount>
          # omitted elements
        </jour:GlInterface>

運行函數結果后輸出:

        <jour:GlInterface>
          # omitted elements
          <jour:EnteredCrAmount currencyCode="USD">1.000000</jour:EnteredCrAmount>
          # omitted elements
        </jour:GlInterface>
        <jour:GlInterface>
          # omitted elements
          <jour:EnteredDrAmount currencyCode="USD">1.000000</jour:EnteredDrAmount>
          # omitted elements
        </jour:GlInterface>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM