Xml 使用参数加载节点同级的xsl模板

Xml 使用参数加载节点同级的xsl模板,xml,xslt,Xml,Xslt,我有一个使用许多变量计算的表。在计算过程中,我将这些计算记录在一个xml文件中 问题是我需要在不同的布局中格式化这个xml 一个从发票开始 另一个从列名开始 在每个“发票”之前,都指定了一个“成本”,它可以是发票价值的一部分,并且每个“成本”都放在表中的一列中 我的XML如下所示: <table> <invoices> <invoice id="1" value="230" supplier="First supplier"/>

我有一个使用许多变量计算的表。在计算过程中,我将这些计算记录在一个xml文件中

问题是我需要在不同的布局中格式化这个xml

  • 一个从发票开始
  • 另一个从列名开始
在每个“发票”之前,都指定了一个“成本”,它可以是发票价值的一部分,并且每个“成本”都放在表中的一列中

我的XML如下所示:

<table>
    <invoices>
        <invoice id="1" value="230" supplier="First supplier"/>
    </invoices>
    <costs>
        <cost id="1" invoice="1" column="2" value="100">
            <calculation>
                <tenant name="Tenant1" cost="30" />
                <tenant name="Tenant2" cost="70" />
            </calculation>
        </cost>
        <cost id="2" invoice="1" column="1" value="130">
            <calculation>
                <tenant name="Tenant1" cost="50" />
                <tenant name="Tenant2" cost="50" />
            </calculation>
            <calculation>
                <tenant name="Tenant1" cost="10" />
                <tenant name="Tenant2" cost="20" />
            </calculation>
        </cost>
    </costs>
    <columns>
        <column id="1" name="Column name 1"/> 
        <column id="2" name="Column name 2"/> 
    </columns>
</table>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
            <xsl:for-each select="table/costs/cost">
                <xsl:call-template name="cost"></xsl:call-template>
                <h1>Mere</h1>
            </xsl:for-each>
        </html>
    </xsl:template>
    <xsl:template match="cost" name="cost">
        <xsl:apply-templates select="invoice"></xsl:apply-templates>
        <h1>Cheltuiala <xsl:value-of select="@id"></xsl:value-of></h1>
        <ul>
            <li>
            <xsl:call-template name="invoice" >
                <xsl:with-param name="idInvoice" select="@invoice"></xsl:with-param>
            </xsl:call-template>
            </li>
            <li>Si ceva avcolo</li>
        </ul>
    </xsl:template>
    <xsl:template name="invoice" match="table/invioces/invoice[@id=@idInvoice]">
        <xsl:param name="idInvoice"></xsl:param>
        <p>Found something</p>
        <h2><xsl:value-of select="@value" /></h2>
    </xsl:template>
</xsl:stylesheet>

我的XSL如下所示:

<table>
    <invoices>
        <invoice id="1" value="230" supplier="First supplier"/>
    </invoices>
    <costs>
        <cost id="1" invoice="1" column="2" value="100">
            <calculation>
                <tenant name="Tenant1" cost="30" />
                <tenant name="Tenant2" cost="70" />
            </calculation>
        </cost>
        <cost id="2" invoice="1" column="1" value="130">
            <calculation>
                <tenant name="Tenant1" cost="50" />
                <tenant name="Tenant2" cost="50" />
            </calculation>
            <calculation>
                <tenant name="Tenant1" cost="10" />
                <tenant name="Tenant2" cost="20" />
            </calculation>
        </cost>
    </costs>
    <columns>
        <column id="1" name="Column name 1"/> 
        <column id="2" name="Column name 2"/> 
    </columns>
</table>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
            <xsl:for-each select="table/costs/cost">
                <xsl:call-template name="cost"></xsl:call-template>
                <h1>Mere</h1>
            </xsl:for-each>
        </html>
    </xsl:template>
    <xsl:template match="cost" name="cost">
        <xsl:apply-templates select="invoice"></xsl:apply-templates>
        <h1>Cheltuiala <xsl:value-of select="@id"></xsl:value-of></h1>
        <ul>
            <li>
            <xsl:call-template name="invoice" >
                <xsl:with-param name="idInvoice" select="@invoice"></xsl:with-param>
            </xsl:call-template>
            </li>
            <li>Si ceva avcolo</li>
        </ul>
    </xsl:template>
    <xsl:template name="invoice" match="table/invioces/invoice[@id=@idInvoice]">
        <xsl:param name="idInvoice"></xsl:param>
        <p>Found something</p>
        <h2><xsl:value-of select="@value" /></h2>
    </xsl:template>
</xsl:stylesheet>

仅仅的
龟属
  • 西塞瓦阿夫科洛酒店
发现了什么

因此,我要做的是基于节点的属性调用模板,显示数据并继续到下一个节点


这可能吗?

您可以使用
current()
函数引用当前上下文节点。但要做到这一点,您需要在调用模板时或之前选择节点:

<xsl:template name="cost">
    ...
    <xsl:for-each match="/table/invoices/invoice[@id=current()/@invoice]">
        <xsl:call-template name="invoice"/>
    </xsl:for-each>
    ...
</xsl:template>
如果需要多次使用标记名,可以使用以下模式:

<xsl:template match="cost">
    ...
    <xsl:apply-templates match="/table/invoices/invoice[@id=current()/@invoice]"
                         mode="cost-invoice"/>
    ...
</xsl:template>

<xsl:template mode="cost-invoice" match="invoice">
    ...
</xsl:template>

...
...
...

您忘记提供转换所需的确切输出。请编辑问题并提供必要的信息。
<xsl:template match="cost">
    ...
    <xsl:apply-templates match="/table/invoices/invoice[@id=current()/@invoice]"
                         mode="cost-invoice"/>
    ...
</xsl:template>

<xsl:template mode="cost-invoice" match="invoice">
    ...
</xsl:template>