0

我有一个结构如下所示的 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>'

来自气泡图 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>'
4

1 回答 1

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>
于 2013-09-28T18:20:16.490 回答