我有一个xml文件,其结构如下所示。
问题:如何选择/查询带子项的元素,其中子项的总和(金额)不等于自己的金额?
没有固定数量的孩子,甚至有没有孩子的元素,这些后来的元素不应出现在选择中。
json bubbletree文件引起的文件结构:
'<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="JSON">
<xs:complexType>
<xs:sequence>
<xs:element ref="label"/>
<xs:element ref="amount"/>
<xs:element ref="color"/>
<xs:element maxOccurs="unbounded" ref="children"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="label" type="xs:string"/>
<xs:element name="amount" type="xs:integer"/>
<xs:element name="color" type="xs:string"/>
<xs:element name="children">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="amount"/>
<xs:element ref="children"/>
<xs:element ref="color"/>
<xs:element ref="label"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>'
来自bubblechart json文件。
我正在使用以下xml文件中标签以'x'开头的元素(手动放入澄清,不在真实文件中):
'<?xml version="1.0" encoding="UTF-8"?>
<JSON>
<label>total</label>
<amount>30</amount>
<children>
<label>x 1</label>
<amount>10</amount>
<children>
<label>1.1</label>
<amount>5</amount>
</children>
<children>
<label>1.2</label>
<amount>6</amount>
</children>
</children>
<children>
<label>2</label>
<amount>10</amount>
<children>
<label>2.1</label>
<amount>5</amount>
</children>
<children>
<label>x 2.2</label>
<amount>5</amount>
<children>
<label>2.2.1</label>
<amount>3</amount>
</children>
<children>
<label>2.2.2</label>
<amount>1</amount>
</children>
<children>
<label>2.2.3</label>
<amount>3</amount>
</children>
</children>
</children>
<children>
<label>x 3</label>
<amount>10</amount>
<children>
<label>3.1</label>
<amount>5</amount>
</children>
<children>
<label>3.2</label>
<amount>6</amount>
</children>
</children>
</JSON>'
答案 0 :(得分:1)
尝试使用此表达式选择所有子元素,子元素本身,其中金额不等于子元素总和
<xsl:apply-templates select="//children[children][amount != sum(children/amount)]" />
例如,使用此XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//children[children][amount != sum(children/amount)]" />
</xsl:template>
<xsl:template match="children">
<children><xsl:value-of select="label" /></children>
</xsl:template>
</xsl:stylesheet>
选择以下儿童
<children>x 1</children>
<children>x 2.2</children>
<children>x 3</children>