我們通過使用 xslt 將 Peppol 供應商發票轉換為另一種 xml 格式,將 Peppol 供應商發票匯入我們的 ERP 系統。此類發票的示例如下:Github:OpenPEPPOL / peppol-bis-invoice-3
我想讓我的會計師完全自由地決定在每個 ERP 供應商發票日記帳欄位中填充哪些欄位。我想讓他們從一組預定義的節點中選擇使用,例如:
/Invoice/ID /Invoice/IssueDate
/Invoice/AccountingSupplierParty/Party/PartyName/Name
/Invoice/InvoiceLine/InvoicedQuantity
/Invoice/InvoiceLine/Item/Name
我從 ERP 系統匯入特定的供應商資訊以在轉換程序中使用,例如查找供應商帳戶:
<vendors>
<vendor>
<administration>YIT</administration>
<FISCALCODE>04705810150</FISCALCODE>
<accountNumber>20003</accountNumber>
<Offset_LedgerAccount>67123</Offset_LedgerAccount>
<name>A.Manzoni&C. Spa</name>
</vendor>
<vendors>
我使用下面的 xslt 在供應商 xml 表中查找此資訊:
<xsl:variable name="LegalEntity">
<xsl:choose>
<xsl:when test="contains(cac:AccountingCustomerParty/cac:Party/cac:PartyName/cbc:Name, 'Italia')">YIT</xsl:when>
<xsl:otherwise>'UNKNOWN LegalEntity'</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="current-vendor-name" select="cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name"/>
<xsl:variable name="vendor-data" select="document('Vendors.xml')/vendors/vendor[name=$current-vendor-name and administration=$LegalEntity]"/>
轉換的輸出(當應用于鏈接中的示例輸入檔案時)當前如下所示:
<?xml version="1.0" encoding="utf-8"?>
<PurchaseInvoices_version_1.0>
<PurchaseInvoice>
<LegalEntity>YIT</LegalEntity>
<SupplierAccountNum>20202</SupplierAccountNum>
<SupplierBankAccount>IBAN32423940</SupplierBankAccount>
<SupplierName>SupplierTradingName Ltd.</SupplierName>
<SupplierCity>London</SupplierCity>
<CurrencyCode>EUR</CurrencyCode>
<AmountExclTax>1325</AmountExclTax>
<AmountInclTax>1656.25</AmountInclTax>
<TaxAmount>331.25</TaxAmount>
<InvoiceId>Snippet1</InvoiceId>
<InvoiceDate>13-11-2017</InvoiceDate>
<PaymentNote>Payment within 10 days, 2% discount</PaymentNote>
<TaxCode>S</TaxCode>
<Lines>
<Line>
<Item>|Description of item</Item>
<Quantity>7</Quantity>
<UnitPrice>400</UnitPrice>
<LineAmount>2800</LineAmount>
<AmountIncl>3500</AmountIncl>
<AmountExcl>2800</AmountExcl>
<TaxAmount>700</TaxAmount>
<TaxCode>S</TaxCode>
<Description>Description of item|2017-11-13</Description>
<Unit>pcs</Unit>
</Line>
<Line>
<Item>|Description 2</Item>
<Quantity>-3</Quantity>
<UnitPrice>500</UnitPrice>
<LineAmount>-1500</LineAmount>
<AmountIncl>-1875</AmountIncl>
<AmountExcl>-1500</AmountExcl>
<TaxAmount>-375</TaxAmount>
<TaxCode>S</TaxCode>
<Description>Description 2|2017-11-13</Description>
<Unit>pcs</Unit>
</Line>
</Lines>
</PurchaseInvoice>
</PurchaseInvoices_version_1.0>
我想在描述欄位中使用輸入 xml 中的哪些文本(節點)以及顯示它們的順序方面創造更多自由。這應該在供應商卡上為每個供應商定義并匯入到上面顯示的示例的 vendor.xml 檔案中。
例如,對于供應商 A,描述可以是所有節點的串聯:
/Invoice/ID /Invoice/IssueDate
/Invoice/InvoiceLine/Item/Name
/Invoice/InvoiceLine/InvoicedQuantity
/Invoice/AccountingSupplierParty/Party/PartyName/Name
而對于供應商 B,描述可能只是:
/Invoice/InvoiceLine/Item/Name
問題是如何從這里開始。
我目前的想法是根據固定數量的變數創建描述:
<Description><xsl:value-of select="$A"/><xsl:value-of select="$B"/><xsl:value-of select="$C"/><xsl:value-of select="$D"/></Description>
并根據 Vendor 卡上的某種輸入將正確的節點放入每個變數中,該輸入被匯入到 vendor.xml 檔案中并在 xslt 腳本中使用。
uj5u.com熱心網友回復:
按照您最初的想法,即使用數字代碼作為描述中包含的值的順序,請考慮以下非常簡化的示例。
XML
<Invoice>
<ID>001</ID>
<DueDate>2002-01-01</DueDate>
<LineItem>
<Name>Widget</Name>
<Quantity>5</Quantity>
<Description>Description of widget</Description>
</LineItem>
<LineItem>
<Name>Gadget</Name>
<Quantity>17</Quantity>
<Description>Description of gadget</Description>
</LineItem>
</Invoice>
XSLT 1.0
<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"/>
<xsl:param name="reorder">030102</xsl:param>
<xsl:template match="/Invoice">
<PurchaseInvoice Id="{ID}">
<!-- ... -->
<xsl:for-each select="LineItem">
<Line>
<!-- ... -->
<Description>
<xsl:call-template name="reorder">
<xsl:with-param name="nodes" select="../DueDate | Name | Description"/>
<xsl:with-param name="order" select="$reorder"/>
</xsl:call-template>
</Description>
</Line>
</xsl:for-each>
</PurchaseInvoice>
</xsl:template>
<xsl:template name="reorder">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="order"/>
<xsl:value-of select="$nodes[number(substring($order, 1 , 2))]"/>
<xsl:if test="string-length($order) > 2">
<xsl:text> | </xsl:text>
<!-- recursive call -->
<xsl:call-template name="reorder">
<xsl:with-param name="nodes" select="$nodes"/>
<xsl:with-param name="order" select="substring($order, 3)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
結果
<?xml version="1.0" encoding="UTF-8"?>
<PurchaseInvoice Id="001">
<Line>
<Description>Description of widget | 2002-01-01 | Widget</Description>
</Line>
<Line>
<Description>Description of gadget | 2002-01-01 | Gadget</Description>
</Line>
</PurchaseInvoice>
為了簡單起見,您必須nodes按照它們在輸入 XML 中出現的順序將數字代碼分配給引數中列出的值。
在這個例子中,代碼是一個全域引數;在您的實作中,您將希望從供應商檔案中檢索它。
添加:
如果您愿意,可以使用描述性字串而不是數字代碼來選擇訂單。但隨后樣式表變得更加復雜:
XSLT 1.0
<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"/>
<xsl:param name="reorder">description|due date|name</xsl:param>
<xsl:template match="/Invoice">
<PurchaseInvoice Id="{ID}">
<!-- ... -->
<xsl:for-each select="LineItem">
<Line>
<!-- ... -->
<Description>
<xsl:call-template name="reorder">
<xsl:with-param name="order" select="$reorder"/>
</xsl:call-template>
</Description>
</Line>
</xsl:for-each>
</PurchaseInvoice>
</xsl:template>
<xsl:template name="reorder">
<xsl:param name="order"/>
<xsl:param name="delimiter" select="'|'"/>
<xsl:variable name="token" select="substring-before(concat($order, $delimiter), $delimiter)" />
<xsl:choose>
<xsl:when test="$token='due date'">
<xsl:value-of select="../DueDate"/>
</xsl:when>
<xsl:when test="$token='name'">
<xsl:value-of select="Name"/>
</xsl:when>
<xsl:when test="$token='description'">
<xsl:value-of select="Description"/>
</xsl:when>
</xsl:choose>
<xsl:if test="contains($order, $delimiter)">
<xsl:text> | </xsl:text>
<!-- recursive call -->
<xsl:call-template name="reorder">
<xsl:with-param name="order" select="substring-after($order, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515930.html
標籤:xml变量xslt
