如何在不同位置获取同名节点中的最后一个值?



大家早安,

我有一个问题,其中包含一个在不同地方重复出现的节点......cac:InvoiceLine 是可变的,可以大于或小于 4。我想要的是代码总是从 LAST cac:InvoiceLine/cac:Item/cbc:Description中获取结果/值的最后 4 个字符。在这种情况下是 6.00 我该怎么做?

这是我得到的XML文件:

<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" udt="urn:un:unece:uncefact:data:draft:UnqualifiedDataTypesSchemaModule:2" cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" stat="urn:oasis:names:specification:ubl:schema:xsd:DocumentStatusCode-1.0" ccts="urn:oasis:names:specification:ubl:schema:xsd:CoreComponentParameters-2">
<UBLVersionID>2.1</UBLVersionID>
<CustomizationID>urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol5a:ver2.0:extended:e-fff:ver3.0</CustomizationID>
<ProfileID>FFF.BE Teamleader 1.0</ProfileID>
<ID>192972</ID>
<IssueDate>2019-11-30</IssueDate>
<InvoiceTypeCode listID="UNCL1001">380</InvoiceTypeCode>
<Note>Onze referentie: 5573395</Note>
<TaxPointDate>2019-11-30</TaxPointDate>
<DocumentCurrencyCode listID="ISO4217">EUR</DocumentCurrencyCode>
<LineCountNumeric>4</LineCountNumeric>
<AdditionalDocumentReference>
<ID>78616_1575288029_TL_MXEfAC</ID>
<DocumentType>CommercialInvoice</DocumentType>
<Attachment>
<EmbeddedDocumentBinaryObject> image here </EmbeddedDocumentBinaryObject>
</Attachment>
</AdditionalDocumentReference>
<AdditionalDocumentReference>
<ID>eFFF</ID>
<DocumentType>Teamleader CRM 1.0</DocumentType>
</AdditionalDocumentReference>
<AccountingSupplierParty>
<Party>
<EndpointID schemeID="BE:CBE"></EndpointID>
<PartyIdentification>
<ID schemeID="BE:CBE"></ID>
</PartyIdentification>
<PartyName>
<Name>test</Name>
</PartyName>
<PostalAddress>
<StreetName>test</StreetName>
<CityName>test</CityName>
<PostalZone>4444</PostalZone>
<Country>
<IdentificationCode listID="ISO3166-1:Alpha2">BE</IdentificationCode>
</Country>
</PostalAddress>
<PartyTaxScheme>
<CompanyID schemeID="BE:VAT">blabla</CompanyID>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</PartyTaxScheme>
<PartyLegalEntity>
<CompanyID schemeID="BE:VAT">blabla</CompanyID>
</PartyLegalEntity>
</Party>
</AccountingSupplierParty>
<AccountingCustomerParty>
<Party>
<EndpointID schemeID="BE:CBE">blabla</EndpointID>
<PartyIdentification>
<ID schemeID="BE:CBE">blabla</ID>
</PartyIdentification>
<PartyName>
<Name>test 2</Name>
</PartyName>
<PostalAddress>
<StreetName>test2</StreetName>
<CityName>testtest</CityName>
<PostalZone>1800</PostalZone>
<Country>
<IdentificationCode listID="ISO3166-1:Alpha2">BE</IdentificationCode>
</Country>
</PostalAddress>
<PartyTaxScheme>
<CompanyID schemeID="BE:VAT">Blablabla</CompanyID>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</PartyTaxScheme>
<PartyLegalEntity>
<CompanyID schemeID="BE:VAT">blablabla</CompanyID>
</PartyLegalEntity>
</Party>
</AccountingCustomerParty>
<PaymentMeans>
<PaymentMeansCode listID="UNCL4461" listName="Payment Means" listURI="http://docs.oasis-open.org/ubl/os-UBL-2.0-update/cl/gc/default/PaymentMeansCode-2.0.gc">1</PaymentMeansCode>
<PaymentDueDate>2019-12-30</PaymentDueDate>
<InstructionID>007113333029</InstructionID>
<PaymentID>007113333029</PaymentID>
<PayeeFinancialAccount>
<ID schemeID="IBAN">BE243343523452345</ID>
<FinancialInstitutionBranch>
<FinancialInstitution>
<ID schemeID="BIC">GKCCBEBB</ID>
</FinancialInstitution>
</FinancialInstitutionBranch>
</PayeeFinancialAccount>
</PaymentMeans>
<TaxTotal>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">377.25</TaxableAmount>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxCategory>
<ID>E</ID>
<Name>45</Name>
<Percent>0</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<LegalMonetaryTotal>
<LineExtensionAmount currencyID="EUR">377.25</LineExtensionAmount>
<TaxExclusiveAmount currencyID="EUR">377.25</TaxExclusiveAmount>
<TaxInclusiveAmount currencyID="EUR">377.25</TaxInclusiveAmount>
<PayableAmount currencyID="EUR">377.25</PayableAmount>
</LegalMonetaryTotal>
<InvoiceLine>
<ID>1</ID>
<InvoicedQuantity>1.0000</InvoicedQuantity>
<LineExtensionAmount currencyID="EUR">71.50</LineExtensionAmount>
<TaxTotal>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">71.50</TaxableAmount>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxCategory>
<ID>E</ID>
<Name>45</Name>
<Percent>0</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>Goods delivered</Description>
</Item>
<Price>
<PriceAmount currencyID="EUR">71.5000</PriceAmount>
</Price>
</InvoiceLine>
<InvoiceLine>
<ID>2</ID>
<InvoicedQuantity>1.0000</InvoicedQuantity>
<LineExtensionAmount currencyID="EUR">101.75</LineExtensionAmount>
<TaxTotal>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">101.75</TaxableAmount>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxCategory>
<ID>E</ID>
<Name>45</Name>
<Percent>0</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>Other costs</Description>
</Item>
<Price>
<PriceAmount currencyID="EUR">101.7500</PriceAmount>
</Price>
</InvoiceLine>
<InvoiceLine>
<ID>3</ID>
<InvoicedQuantity>1.0000</InvoicedQuantity>
<LineExtensionAmount currencyID="EUR">204.00</LineExtensionAmount>
<TaxTotal>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">204.00</TaxableAmount>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxCategory>
<ID>E</ID>
<Name>45</Name>
<Percent>0</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>BLADIEBLAD</Description>
</Item>
<Price>
<PriceAmount currencyID="EUR">204.0000</PriceAmount>
</Price>
</InvoiceLine>
<InvoiceLine>
<ID>4</ID>
<InvoicedQuantity>0.0000</InvoicedQuantity>
<LineExtensionAmount currencyID="EUR">0.00</LineExtensionAmount>
<TaxTotal>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxSubtotal>
<TaxableAmount currencyID="EUR">0.00</TaxableAmount>
<TaxAmount currencyID="EUR">0.00</TaxAmount>
<TaxCategory>
<ID>E</ID>
<Name>45</Name>
<Percent>0</Percent>
<TaxScheme>
<ID>VAT</ID>
</TaxScheme>
</TaxCategory>
</TaxSubtotal>
</TaxTotal>
<Item>
<Description>123456.00</Description>
</Item>
<Price>
<PriceAmount currencyID="EUR">0.0000</PriceAmount>
</Price>
</InvoiceLine>
</Invoice>

我试过这个:

<xsl:template match="/">
<xsl:for-each select="/x:Invoice/cac:InvoiceLine/cac:Item">
<xsl:if test="cbc:Description"><xsl:value-of select="substring(cbc:Description, string-length(cbc:Description) - 3)" /></xsl:if>
</xsl:for-each>
</xsl:template>

这个从描述中的所有值中获取最后 4 个字符,这些值位于不同的 IncoiceLines/Item 中。我只想要最后一个。我无法连接到唯一的 cac:ID,因为这也是可变的。我总是希望它获得此路径中的最后一个值。我该怎么做?

试试这个:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.vb.org/someNS"
xmlns:cbc="http://www.vb.org/someNS"
xmlns:cac="http://www.vb.org/someNS"
version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="descendant::*:Description[not(following::*:Description)]">
<xsl:value-of select="substring(., string-length(.) - (string-length(.) - (string-length(.)-3)))"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

为什么不简单地:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:inv="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
exclude-result-prefixes="inv">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="last-desc" select="inv:Invoice/inv:InvoiceLine[last()]/inv:Item/inv:Description"/>
<output>
<xsl:value-of select="substring($last-desc, string-length($last-desc)-3)"/>
</output>
</xsl:template>
</xsl:stylesheet>

请注意,这假定您需要发票最后一项的描述。如果并非所有项目都有描述,并且您希望最后一个项目有描述,请使用:

<xsl:variable name="last-desc" select="(inv:Invoice/inv:InvoiceLine/inv:Item/inv:Description)[last()]"/>

附言我不明白为什么你需要有 3 个不同的前缀。整个 XML 位于单个命名空间中;

相关内容

  • 没有找到相关文章

最新更新