Xml Xpath:其父级包含1个“abc”元素的“abc”元素数

Xml Xpath:其父级包含1个“abc”元素的“abc”元素数,xml,xslt,xpath,Xml,Xslt,Xpath,假设我有以下XML: <elementList id="1"> <everything> <owned>no</owned> </everything> <elementList id="2"> <allElements> <owned>no</owned> </allElements&g

假设我有以下XML:

<elementList id="1">
    <everything>
        <owned>no</owned>
    </everything>
    <elementList id="2">
        <allElements>
            <owned>no</owned>
        </allElements>
        <officeList id="3">
            <room>
                <owned>no</owned>
            </room>
            <furniture>
                <owned>yes</owned>
            </furniture>
        </officeList>
    </elementList>
    <transportation>
        <allCarsOwned>
            <owned>yes</owned>
        </allCarsOwned>
        <cars id="4">
            <truck>
                <owned>yes</owned>
            </truck>
            <motorcycle>
                <owned>yes</owned>
            </motorcycle>
            <familyCar>
                <owned>yes</owned>
            </familyCar>
        </cars>
    </transportation>
</elementList>
同样,非自有元素包括:

furniture, truck, motorcycle, familyCar
room
以下元素不应包含在任何列表中:

everything, allElements, allCarsOwned
名字可能会有很多不同,没有规则,我不能通过名字来识别它们

目前,我计算所有拥有和未拥有的物品,如下所示:

    <xsl:variable name="ownedStuff" select="count(//doc:owned[text()='yes'])"/>
    <xsl:variable name="notOwnedStuff" select="count(//doc:owned[text()='no'])"/>
或者别的什么。。我真的不明白

有人能帮我吗

.
.
.
绘图更改:对于每个拥有/未拥有的节点,是否很难获得整行ID和父节点名称


谢谢大家!

如果我理解正确,您需要计算以下元素:

拥有一个孩子,其价值=是;和 除了自己的侄子,没有其他侄子 -

恐怕我不明白这部分:

情节变化:整行ID会很难吗 以及每个已拥有/未拥有节点的父节点名称


多亏了谷歌和空闲时间,我自己找到了这两种解决方案

首先,我要感谢@BallaR回答stackoverflow.com上的另一个问题:

他对这个问题的回答是:

count(//ComRequest/root/component[count(compLine)>10])
<> P>让我考虑使用复杂的选择,这是个骗局!!!感谢上帝,这样的想法是可能的。。。我使用XSLT已经5年多了,真不敢相信我这么快就忘记了

使用复合过滤器计数节点的解决方案是:

count(//doc:owned[text()='whatever' and count(../..//doc:owned[text()='whatever']) lt 2])
现在我必须看看我是否达到了枚举每个所属元素的所有祖先的程度

.
.
.
事实上,在我的例子中,还可以获得任何拥有的节点的所有祖先的名称,这些节点通常不属于我:

<xsl:for-each select="//doc:owned[text()='whatever' and count(../..//doc:owned[text()='whatever']) lt 2]">
    <xsl:value-of select="position()"/>. 
    <xsl:value-of select="name(..)"/>
    <xsl:if test="../@Id">
        (#<xsl:value-of select="../@Id"/>)
    </xsl:if>
    <xsl:call-template name="while">
        <xsl:with-param name="parentNode" select="../.."/>
    </xsl:call-template>
</xsl:for-each>

<xsl:template name="while">
    <xsl:param name="parentNode"/>
    <xsl:if test="name($parentNode) != ''"> <!-- to avoid root and document nodes -->
        [<xsl:value-of select="name($parentNode)"/>
        <xsl:if test="$parentNode/../@Id">
            (#<xsl:value-of select="$parentNode/../@Id"/>)
        </xsl:if>]
        <xsl:if test="$parentNode/..">
            <xsl:call-template name="while">
                <xsl:with-param name="parentNode" select="$parentNode/.."/>
            </xsl:call-template>
        </xsl:if>
    </xsl:if>
</xsl:template>
我很高兴XSLT的存在:


向大家问好

注意:列表不一定包含所有参数,有些缺少ID,有些包含其他参数。。。这太糟糕了你怎么把评论格式化?!?!?!?!我想计算所有的终端元素,这些元素不包含其他元素,无论是否拥有。情节发生变化的部分指的不是简单地拥有这两个列表:拥有的:家具、卡车、摩托车、家庭汽车未拥有的:房间,而是拥有的每个元素为:拥有的:元素列表->元素列表->家具、元素列表->运输->汽车->卡车、元素列表->运输->汽车->摩托车,元素列表->交通->汽车->家庭汽车未拥有:元素列表->元素列表->room@ro0ter你如何格式化评论?!?!?!你没有;你改为编辑你的问题。
.
.
.
<xsl:for-each select="//doc:owned[text()='whatever' and count(../..//doc:owned[text()='whatever']) lt 2]">
    <xsl:value-of select="position()"/>. 
    <xsl:value-of select="name(..)"/>
    <xsl:if test="../@Id">
        (#<xsl:value-of select="../@Id"/>)
    </xsl:if>
    <xsl:call-template name="while">
        <xsl:with-param name="parentNode" select="../.."/>
    </xsl:call-template>
</xsl:for-each>

<xsl:template name="while">
    <xsl:param name="parentNode"/>
    <xsl:if test="name($parentNode) != ''"> <!-- to avoid root and document nodes -->
        [<xsl:value-of select="name($parentNode)"/>
        <xsl:if test="$parentNode/../@Id">
            (#<xsl:value-of select="$parentNode/../@Id"/>)
        </xsl:if>]
        <xsl:if test="$parentNode/..">
            <xsl:call-template name="while">
                <xsl:with-param name="parentNode" select="$parentNode/.."/>
            </xsl:call-template>
        </xsl:if>
    </xsl:if>
</xsl:template>