3

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

<?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 文件:

<?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>

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

<?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>

所以这是我目前得到的输出:

<?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,

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

谢谢!

亚历山大·哈辛托

4

1 回答 1

1

该表达式ecd:propList/(preceding-sibling::ecd:prop[last()])不选择任何内容(一个空的节点集),因为该ecd:propList元素没有任何前面的兄弟名为ecd:prop

于 2018-07-14T09:16:09.107 回答