我有许多可能有也可能没有订单号的文章行。如果该行缺少订单号,我想显示最新的现有订单号作为这篇文章的订单号。
<?xml version="1.0" encoding="utf-8"?>
<Invoice>
<InvoiceNumber>123</InvoiceNumber>
<OrderNumber>head1</OrderNumber>
<Rows>
<Row>
<ArticleNumer>1</ArticleNumer>
<OrderNumber>line1</OrderNumber>
<Quantity>1</Quantity>
<Price>10</Price>
</Row>
<Row>
<ArticleNumer>2</ArticleNumer>
<OrderNumber><!--Here i want line1--></OrderNumber>
<Quantity>1</Quantity>
<Price>10</Price>
</Row>
<Row>
<ArticleNumer>3</ArticleNumer>
<OrderNumber><!--Here i want line1--></OrderNumber>
<Quantity>1</Quantity>
<Price>10</Price>
</Row>
<Row>
<ArticleNumer>4</ArticleNumer>
<OrderNumber>line2</OrderNumber>
<Quantity>1</Quantity>
<Price>10</Price>
</Row>
<Row>
<ArticleNumer>4</ArticleNumer>
<OrderNumber><!--Here i want line2--></OrderNumber>
<Quantity>1</Quantity>
<Price>10</Price>
</Row>
</Rows>
</Invoice>
我在行上使用了一个 for-each,这似乎使整个事情变得复杂了一点。如果第二行为空,通过使用previous-sibling,我可以获得最新的现有值。但是如果第三和第四行缺少订单号,我怎么才能得到呢?
在 if 语句中创建变量不起作用。在 for-each 中使用模板会给我一条错误消息。我还在考虑使用一个计数器,为每个空订单号行添加 1 并使用前兄弟,但在我面前看不到该解决方案。
有任何想法吗?
最好的问候, 安德烈亚斯
这是我使用的更多确切代码:
现在它会更加混乱。特别是当我对 XSLT 非常不熟悉时。Best_nr2 是我样本中的订单号。但:
<xsl:for-each select="Invoice/Tables/Table/TableRow">
<xsl:if test="string-length(Field [@Type='best_nr2']/text())<2 and
string-length(preceding-sibling::TableRow/Field [@Type='best_nr2']/text())<2 ">
<secondaryorder_number>
<xsl:value-of select="$dispatchOrderNr"/>
</secondaryorder_number>
</xsl:if>
<xsl:if test="string-length(Field [@Type='best_nr2']/text())>2">
<secondaryorder_number>
<xsl:value-of select="Field [@Type='best_nr2']/text()"/>
</secondaryorder_number>
</xsl:if>
<xsl:if test="string-length(Field [@Type='best_nr2']/text())<2 and
string-length(preceding-sibling::TableRow/Field [@Type='best_nr2']/text())>2 and
preceding-sibling::TableRow[1]/Field [@Type='best_nr2']/text()>2 ">
<secondaryorder_number>
<xsl:value-of select="preceding-sibling::TableRow[1]/Field [@Type='best_nr2']/text()"/>
</secondaryorder_number>
</xsl:if>
<xsl:if test="string-length(Field [@Type='best_nr2']/text())<2 and
string-length(preceding-sibling::TableRow[1]/Field [@Type='best_nr2']/text())<2
and string-length(preceding-sibling::TableRow/Field[@Type='best_nr2']/text())>2">
<secondaryorder_number>
</secondaryorder_number>
</xsl:if>
<lit_quantity>
<xsl:value-of select="translate(Field [@Type='lit_deliveredquantity'], ' ,',' .')"/>
</lit_quantity>
</LineItem>
<xsl:variable name="dispatchbestnr2" select="Field [@Type='best_nr2']/text()"/>
</xsl:for-each>
</LineItems>
</Dispatchnote>
</xsl:for-each