如果元素在 XSLT 中处于同一级别,则消除重复元素

Eliminating duplicate element if the elements are on the same level in XSLT

我希望在第一个 LoanDetails 元素中显示 ContributionFunds 元素; ContributionFunds 和 LoanDetails 是可重复的元素,因此为了显示它们,我制作了两个模板;

问:有没有办法在指定的路径输出信息? 我将使用 XSLT 1.0 还是 2.0 并不重要。

P.S。来自属性的信息不计算在内。 示例:

P.P.S. It seems that all i was needed was to test the position,

      <xsl:if test="position()=1">
          <xsl:apply-templates select="../ContributionFunds"/></xsl:if> 

i approached the problem wrong from the start.

<root>
    <Data1>
        <Data2>
            <Data..>
                <ContributionFunds Amount="546548" Type="NetProceedsFromSaleOfProperty">
                    <Information../>
                </ContributionFunds>
                <ContributionFunds Amount="10000000" Type="Savings">
                    <Information../>
                </ContributionFunds>
                <LoanDetails ProductName="Variable Home Loan IO" ProductCode="VHLIO">
                    <Information../>
                </LoanDetails>
                <LoanDetails ProductName="Variable Home Loan IO" ProductCode="VHLIO">
                    <Information../>
                 <LoanDetails>
                 <Foo/>
                 <Foo.../>
            </Data..>
        </Data2>
    </Data1>
</root>

如果在 LoanDetails 模板中应用模板 ContributionFunds 获得的输出

<root>
    <Data1>
        <Data2>
            <Data..>
                <LoanDetails ProductName="Variable Home Loan IO" ProductCode="VHLIO2">
                    <ContributionFunds Amount="546548" Type="NetProceedsFromSaleOfProperty">
                       <Information../>
                     </ContributionFunds>
                    <ContributionFunds Amount="10000000" Type="Savings">
                      <Information../>
                     </ContributionFunds>
                      <Information../>
                </LoanDetails>
                <LoanDetails ProductName="Variable Home Loan IO" ProductCode="VHLIO2">
                    <ContributionFunds Amount="546548" Type="NetProceedsFromSaleOfProperty">
                       <Information../>
                     </ContributionFunds>
                    <ContributionFunds Amount="10000000" Type="Savings">
                       <Information../>
                    </ContributionFunds>
                     <Information../>
                </LoanDetails>
                <Foo/>
                <Foo.../>
            </Data..>
        </Data2>
    </Data1>
</root>

期望的输出

<root>
    <Data1>
        <Data2>
            <Data..>
                <LoanDetails ProductName="Variable Home Loan IO" ProductCode="VHLIO2">
                    <ContributionFunds Amount="546548" Type="NetProceedsFromSaleOfProperty">
                        <Information../>
                    </ContributionFunds>
                    <ContributionFunds Amount="10000000" Type="Savings">
                        <Information../>
                     </ContributionFunds>
                      <Information../>
                </LoanDetails>
                <LoanDetails ProductName="Variable Home Loan IO" ProductCode="VHLIO">
                   <Information../>
                <LoanDetails>     
                <Foo/>
                <Foo.../>
            </Data..>
        </Data2>
    </Data1>
</root>

对于我使用的模板:

<xsl:template match="LoanDetails">
    <LoanDetails>
        ...information
        <xsl:apply-templates select="../ContributionFunds"/>
    </LoanDetails>
</xsl:template>

<xsl:template ...>
    ...

    <LoanDetailSegment CombinationLoan="{Overview/@CombinationLoan}">
        <xsl:apply-templates select="LoanDetails"/>
        ...<!--other templates-->
    </LoanDetailSegment>
...
</Application>
</xsl:template><!--Not the root-->

您示例中显示的结果可以通过以下方式实现:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Data..">
    <xsl:copy>
        <xsl:apply-templates select="node()[not(self::ContributionFunds)]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="LoanDetails[1]">
    <xsl:copy>
        <xsl:apply-templates select="@*"/>
        <xsl:apply-templates select="../ContributionFunds"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" method="xml" version="1.0" indent="yes"/>
    <!-- identity template -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <!-- specific part -->
    <xsl:template match="ContributionFunds"/>
    <xsl:template match="ContributionFunds" mode="inline">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="LoanDetails[1]">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:apply-templates select="../ContributionFunds" mode="inline"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>