XML - XSLT - count() 函数内的 document() 函数 [英] XML - XSLT - document() function inside count() function

查看:58
本文介绍了XML - XSLT - count() 函数内的 document() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一篇文章中问了一个类似的问题,但我决定制作这个新问题,因为这是一个不同的问题.我使用 document() 函数使用两个 XML 输入文件来访问其中一个(外部文件).我正在尝试在 count() 函数中使用 document() 函数,但我不知道为什么它不起作用......这是 XML 输入文档:

I asked a similar question on another post but I decided to make this new one since this is a different problem. I am using two XML input files using the document() function to access one of them (the external file). I am trying to use the document() function inside the count() function but I don't know why it is not working... This is the XML input document:

<?xml version="1.0" encoding="UTF-8"?>
<parent>
    <childs>
        <child ID="1" name="John" />
        <child ID="2" name="Marie"/>
        <child ID="3" name="Joseph"/>
    </childs>
</parent>

这是我与 document() 函数一起使用的外部 XML 文件:

This is the external XML file that I use with the document() function:

<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design">
    <property name="units">in</property>
    <text-property name="displayName">Daisy</text-property>
    <text-property name="text">Just plain text</text-property>
    <propList>
        <prop name="prop1"/>
        <prop name="prop2"/>
        <prop name="prop3"/>
        <prop name="prop4"/>
        <prop name="prop5"/>
    </propList>
</report>

所以我想要做的是获取属性值为displayNametext-property元素的值,然后计算的数量prop 元素,产生一个新的 child 元素.这是我的 XSLT 代码:

So what I am trying to do is to get the value of the text-property element which attribute value is displayName, and then count the number of prop elements, producing a new child element. This is my XSLT code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:ecd="http://www.eclipse.org/birt/2005/design"
  exclude-result-prefixes="xs ecd"
  expand-text="yes"
  version="3.0">

    <xsl:output indent="yes" />

    <xsl:mode on-no-match="shallow-copy"/>

    <xsl:template match="parent/childs/child[last()]">

    <xsl:next-match/>
        <child>
            <xsl:attribute name="ID">
                <xsl:value-of select="count(preceding-sibling::child)+2" />
            </xsl:attribute>
            <xsl:attribute name="name">
                <xsl:value-of select="document('inputStack.xml')/ecd:report/ecd:text-property[@name = 'displayName']"/>
            </xsl:attribute>
            <!--new attribute-->
            <xsl:attribute name="nProps">
                <xsl:value-of select="count(document('inputStack.xml')/ecd:report/ecd:propList/(preceding-sibling::ecd:prop[last()]))+1"/>
            </xsl:attribute>
        </child>
    </xsl:template>

</xsl:stylesheet>

这是我目前得到的输出:

So this is the output I'm getting at the moment:

<?xml version="1.0" encoding="UTF-8"?>
<parent>
    <childs>
        <child ID="1" name="John"/>
        <child ID="2" name="Marie"/>
        <child ID="3" name="Joseph"/>
        <child ID="4" name="Daisy" nProps="1"/>
    </childs>
</parent>

如你所见,我得到了属性 name 的正确值 (Daisy) 但属性 nProps 的值是错误的,因为它应该是5、

As you can see, I'm getting the value of the attribute name right (Daisy) but the value of the attribute nProps is wrong, as it should be 5,

我在 count 函数内的 XPATH 中做错了什么吗?

Am I doing anything wrong in the XPATH inside the count function?

谢谢!

亚历山大·哈辛托

推荐答案

表达式 ecd:propList/(preceding-sibling::ecd:prop[last()])node-set) 因为 ecd:propList 元素没有任何前面的名为 ecd:prop 的兄弟元素.

The expression ecd:propList/(preceding-sibling::ecd:prop[last()]) selects nothing (an empty node-set) because the ecd:propList element does not have any preceding siblings named ecd:prop.

这篇关于XML - XSLT - count() 函数内的 document() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆