输入:-
<b>
<cac:OrderLine>
<cac:LineItem>
<cbc:ID>1</cbc:ID>
<cbc:Quantity unitCode="KGM">100</cbc:Quantity>
<cbc:TotalTaxAmount currencyID="GBP">17.50</cbc:TotalTaxAmount>
<cac:Price>
<cbc:PriceAmount currencyID="GBP">100.00</cbc:PriceAmount>
<cbc:BaseQuantity unitCode="KGM">1</cbc:BaseQuantity>
</cac:Price>
</cac:LineItem>
</cac:OrderLine>
<cac:OrderLine>
<cac:LineItem>
<cbc:ID>5</cbc:ID>
<cbc:Quantity unitCode="KGM">300</cbc:Quantity>
<cbc:TotalTaxAmount currencyID="GBP">100.50</cbc:TotalTaxAmount>
<cac:Price>
<cbc:PriceAmount currencyID="GBP">2000.00</cbc:PriceAmount>
<cbc:BaseQuantity unitCode="KGM">52</cbc:BaseQuantity>
</cac:Price>
</cac:LineItem>
</cac:OrderLine>
实际上,这是输入结构,如果我们像这样应用循环
<xsl:for-each select="b/OrderLine/LineItem">它应该只选择第一个行项目,但它选择了输入中存在的所有行项目?有人能在这里解释一下xsl:for-each的功能吗?
因为循环应该应用于upto to Order本身,但上面编写的代码如何挑选输入文件中存在的所有行项目
发布于 2014-02-11 18:12:16
如果您只关注第一个LineItem,请不要使用xsl:for-each。使用
<xsl:template match="b/OrderLine/LineItem[1]">
....
</xsl:template>顾名思义,for-each处理与select表达式匹配的每一项
发布于 2014-02-11 19:07:40
非常具体地回答你的问题:
1)你没有提到在哪里
<b>即将结束
2)如果您的xpath选择了所有节点,那么它肯定是文档的根节点。
3) <xsl:for-each select="b/OrderLine/LineItem">
名称本身证明了逻辑将应用于xpath所指向的"EACH“元素
发布于 2014-02-11 18:09:37
你的XPath会说:“找到所有的b节点,然后是所有的OrderLine节点,然后是所有的LineItem节点。是的,这是预期的功能。”
旁注:你应该使用模板而不是for-each。(ForEach是开发人员从其他语言使用XSLT时所使用的语言。)
如果您只需要每个OrderLine中的第一个LineItem节点,则快速修改为:
<xsl:for-each select="b/OrderLine/LineItem[1]">Playground。
https://stackoverflow.com/questions/21698664
复制相似问题