XSLT-如何查找属性值存在于不同节点列表中的一个节点列表的属性之和
我遇到了一个XSLT1.0样式表的问题,我认为这个样式表可能相当简单,但我似乎无法完全理解它。我有一个XML,看起来像这样XSLT-如何查找属性值存在于不同节点列表中的一个节点列表的属性之和,xslt,Xslt,我遇到了一个XSLT1.0样式表的问题,我认为这个样式表可能相当简单,但我似乎无法完全理解它。我有一个XML,看起来像这样 <Components> <Component Quantity="2.0" MaterialCode="1111"/> <DispensingInstructions/> <ChargeContainers> <Ch
<Components>
<Component Quantity="2.0" MaterialCode="1111"/>
<DispensingInstructions/>
<ChargeContainers>
<ChargeContainer BarcodeValue="def"/>
<ChargeContainer BarcodeValue="jkl"/>
</ChargeContainers>
<Comments/>
<Transactions/>
<DispenseContainers>
<Container BarcodeValue="abc" Quantity="1.0"></Container>
<Container BarcodeValue="def" Quantity="2.0"></Container>
<Container BarcodeValue="ghi" Quantity="3.0"></Container>
<Container BarcodeValue="jkl" Quantity="4.0"></Container>
</DispenseContainers>
</Component>
<Component Quantity="1.0" MaterialCode="12345"/>
<DispensingInstructions/>
<ChargeContainers/>
<Comments/>
<Transactions/>
<DispenseContainers/>
</Component>
</Components>
我试图获取容器的“数量”属性的总和,但前提是容器条形码存在于ChargeContainers中。因此,在本例中,我想要一个返回6的函数,因为ChargeContainers有一个带有条形码“def”的节点和一个带有条形码“jkl”的节点,而DispenceContainers的数量为2和4,总和为6
我有一些XSL看起来像这样:
<xsl:for-each select="//Component">
<!--
Place holder for code to make table row with a bunch of info from nodelists of component
-->
<td>
<xsl:variable name="TotalDispensedQuantity" select="sum(DispenseContainers/Container/@Quantity)" />
<xsl:if test="$TotalDispensedQuantity != number(@Quantity)">
<xsl:attribute name="style">background-color: red;</xsl:attribute>
</xsl:if>
</td>
</xsl:for-each>
背景色:红色;
但现在我必须交叉参考配药容器,以确保它们存在于充电容器中,然后再将其数量添加到总数中。我尝试添加答案中所述的密钥,如下所示:
<xsl:for-each select="//Component">
<!--Get sum of dispensed containers quantities if they exist in charged containers list-->
<xsl:key name="k1" match="//ChargeContainers/ChargeContainer" use="@BarcodeValue" />
<xsl:template match=".">
<result>
<xsl:variable name="TotalChargedQuantity" select = "sum(DispenseContainers/Container[key('k1', @BarcodeValue)]/@Quantity)"/>
</result>
</xsl:template>
<!--
Place holder for code to make table row with a bunch of info from nodelists of component
-->
<td>
<xsl:if test="$TotalChargeQuantity != number(@Quantity)">
<xsl:attribute name="style">background-color: red;</xsl:attribute>
</xsl:if>
</td>
</xsl:for-each>
背景色:红色;
但是,我得到一个错误,指出key不能是for each的子元素。有没有一种循环友好的方法
Edit1:我将重写XML,使其更接近实际,并提供更多的上下文。给出的解决方案可能是正确的,但我遇到了一个错误,key不能是每个的子项。请使用解析交叉引用-例如:
XSLT1.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:key name="k1" match="NodeList1/Node" use="@name" />
<xsl:template match="/Root">
<result>
<xsl:value-of select="sum(NodeList2/Node[key('k1', @name)]/@value)"/>
</result>
</xsl:template>
</xsl:stylesheet>
<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:key name="k1" match="ChargeContainer" use="concat(generate-id(ancestor::Component), '|', @BarcodeValue)" />
<xsl:template match="/Components">
<table>
<tr>
<th>Material Code</th>
<th>Your Result</th>
</tr>
<xsl:for-each select="Component">
<xsl:variable name="component-id" select="generate-id()" />
<tr>
<td>
<xsl:value-of select="@MaterialCode"/>
</td>
<td>
<xsl:value-of select="sum(DispenseContainers/Container[key('k1', concat($component-id, '|', @BarcodeValue))]/@Quantity)"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
演示(使用格式良好的(!)XML作为输入):在所有有关XSLT的问题中,请说明您的处理器支持哪个版本的XSLT。谢谢,我想key可能就是我要找的,但我遇到了一些语法错误。我已经更新了问题,以便更准确地描述情况(以及我在尝试实现您的关键解决方案时遇到的错误)。我将更深入地研究密钥,但如果您看到我的问题是什么,请让我知道:)再次感谢!1.
键
声明必须位于样式表的顶层。2.我要求您说明XSLT的版本,但您一直在隐藏这些信息。这很重要,因为XSLT2.0中的键具有更多功能。这是XSLT 1.0。我也试图对你的答案进行投票,但它说我没有足够的声誉:(抱歉。啊,很好。现在,你的新XML令人困惑。你想为每个组件
获得一个单独的总和,受组件
中列出的ChargeContainers
的限制吗?