XML-XSLT-使用两个XML输入文档

XML-XSLT-使用两个XML输入文档,xml,xslt,xslt-3.0,Xml,Xslt,Xslt 3.0,我有一个小问题,可能很容易解决,但我整个下午都在解决它,我真的不知道如何解决它 基本上,我有以下输入XML文档: <?xml version="1.0" encoding="UTF-8"?> <parent> <childs> <child ID="1" name="John" /> <child ID="2" name="Marie"/> <child ID="3" nam

我有一个小问题,可能很容易解决,但我整个下午都在解决它,我真的不知道如何解决它

基本上,我有以下输入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> 
以下是我的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>

所以
nProps
值应该是5而不是1。。。我在这条路上做错什么了吗?多谢各位


Alexandre Jacinto

对于您可以使用的单个指令

<xsl:value-of xpath-default-namespace="http://www.eclipse.org/birt/2005/design" select="document('inputStack.xml')/report/text-property[@name = 'displayName']"/>

看。如果有更多XSLT元素需要使用名称空间,则可以在公共容器元素上声明
xpath默认名称空间
,但请记住,您希望使用具有不同名称空间的两个文档,因此需要确保需要默认名称空间为空的元素不被覆盖它


根据您的需要,在样式表中使用
为名称空间声明前缀并在需要时使用该前缀来限定元素名
文档('inputStack.xml')/ecd:report/ecd:text属性[@name='displayName']
在XPath表达式中。

对于可以使用的单个指令

<xsl:value-of xpath-default-namespace="http://www.eclipse.org/birt/2005/design" select="document('inputStack.xml')/report/text-property[@name = 'displayName']"/>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs mn"  xmlns:mn="http://www.eclipse.org/birt/2005/design"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:variable name="doc" select="document('date3.xml')"/>
    <xsl:template match="parent">
        <xsl:copy>
            <childs>
                <xsl:for-each select="childs/child">
                <child>
                   <xsl:attribute name="ID">
                       <xsl:value-of select="@ID"/>
                   </xsl:attribute>
                    <xsl:attribute name="name">
                        <xsl:value-of select="@name"/>
                    </xsl:attribute>
                </child>
                </xsl:for-each>
                <child>
                    <xsl:attribute name="ID">
                        <xsl:value-of select="'4'"/>
                    </xsl:attribute>
                    <xsl:attribute name="name">
                        <xsl:value-of select="$doc/mn:report/mn:text-property[@name='displayName']"/>
                    </xsl:attribute> 
                </child>
            </childs>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

看。如果有更多XSLT元素需要使用名称空间,则可以在公共容器元素上声明
xpath默认名称空间
,但请记住,您希望使用具有不同名称空间的两个文档,因此需要确保需要默认名称空间为空的元素不被覆盖它

根据您的需要,在样式表中使用
为名称空间声明前缀,并在需要的地方使用该前缀来限定XPath表达式中的元素名
文档('inputStack.xml')/ecd:report/ecd:text属性[@name='displayName']


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs mn"  xmlns:mn="http://www.eclipse.org/birt/2005/design"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:variable name="doc" select="document('date3.xml')"/>
    <xsl:template match="parent">
        <xsl:copy>
            <childs>
                <xsl:for-each select="childs/child">
                <child>
                   <xsl:attribute name="ID">
                       <xsl:value-of select="@ID"/>
                   </xsl:attribute>
                    <xsl:attribute name="name">
                        <xsl:value-of select="@name"/>
                    </xsl:attribute>
                </child>
                </xsl:for-each>
                <child>
                    <xsl:attribute name="ID">
                        <xsl:value-of select="'4'"/>
                    </xsl:attribute>
                    <xsl:attribute name="name">
                        <xsl:value-of select="$doc/mn:report/mn:text-property[@name='displayName']"/>
                    </xsl:attribute> 
                </child>
            </childs>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>


非常感谢您的回答和解释,我在使用名称空间时仍然遇到一些困难,因此这非常有帮助!嗨,Martin,我在count()函数中使用document()函数时遇到问题。。。我不知道为什么它不起作用。我正在编辑我的问题,向大家展示我的意思,谢谢!请在一个新的、单独的问题中提出新的和不同的问题。很抱歉,我这样做了,新的问题在这里:非常感谢您的回答和解释,我在名称空间方面仍然有一些困难,所以这真的很有帮助!嗨,Martin,我在count()函数中使用document()函数时遇到问题。。。我不知道为什么它不起作用。我正在编辑我的问题,向大家展示我的意思,谢谢!请在一个新的、单独的问题中提出新的和不同的问题。对不起,我这样做了,新的问题在这里:谢谢你的回答!我更新了这个问题,因为我试图在count()函数中使用document()函数。。。你知道我做错了什么吗?谢谢你谢谢你的回答!我更新了这个问题,因为我试图在count()函数中使用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"/>
      <child ID="4" name="Daisy" nProps="1"/>
    </childs>
</parent>
<xsl:value-of xpath-default-namespace="http://www.eclipse.org/birt/2005/design" select="document('inputStack.xml')/report/text-property[@name = 'displayName']"/>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs mn"  xmlns:mn="http://www.eclipse.org/birt/2005/design"
    version="2.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:variable name="doc" select="document('date3.xml')"/>
    <xsl:template match="parent">
        <xsl:copy>
            <childs>
                <xsl:for-each select="childs/child">
                <child>
                   <xsl:attribute name="ID">
                       <xsl:value-of select="@ID"/>
                   </xsl:attribute>
                    <xsl:attribute name="name">
                        <xsl:value-of select="@name"/>
                    </xsl:attribute>
                </child>
                </xsl:for-each>
                <child>
                    <xsl:attribute name="ID">
                        <xsl:value-of select="'4'"/>
                    </xsl:attribute>
                    <xsl:attribute name="name">
                        <xsl:value-of select="$doc/mn:report/mn:text-property[@name='displayName']"/>
                    </xsl:attribute> 
                </child>
            </childs>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>