2016-04-11 1 views
0

Ich freue mich, die ContributionFunds-Elemente im ersten LoanDetails-Element anzuzeigen; ContributionFunds und LoanDetails sind wiederholbare Elemente, also habe ich für die Anzeige zwei Vorlagen erstellt;Eliminieren doppeltes Element, wenn die Elemente in XSLT auf der gleichen Ebene sind

Frage: Gibt es eine Möglichkeit, die Informationen nur im gewünschten Pfad auszugeben? Es spielt keine Rolle, ob ich XSLT 1.0 oder 2.0 verwenden werde.

P.S. Die Information aus den Attributen zählt nicht. Probe:

P.P.S. Es scheint, dass alles, was ich brauchte, war, um die Position zu testen,

ich näherte sich das Problem von Anfang an falsch.

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

Der Ausgang erhalten, wenn die Vorlage in ContributionFunds LoanDetails Vorlage für die template

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

Die gewünschte Ausgangs

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

Anwendung ich verwendet habe:

<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--> 
+0

Das Beispiel verwirrend ist, da es keine Duplikate in der Quelle nicht beseitigen ist. Vermutlich könnte es mehr Elemente als gezeigt geben - wie entscheidet man, welche 'LoanDetails' welche' ContributionFunds' bekommen? –

+0

Es entscheidet nicht und deswegen möchte ich alle ContributionFunds auf die ersten LoanDetails ausgeben. – DanielCSD

+0

Woher kommt der "VHLIO2"? Es ist nicht in Ihrer Beispieleingabe XML. –

Antwort

0

Das Ergebnis in Ihrem Beispiel gezeigt kann erreicht werden durch:

<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> 
+0

Ich denke, die Lösung ist nicht geeignet, da das LoanDetails-Element und ContributionFunds in anderen Elementen verschachtelt sind. Kann das behoben werden ohne xsl: copy? – DanielCSD

+0

@DanielCSD Ich habe meine Antwort auf Ihr bearbeitetes Beispiel angepasst. Dies setzt voraus, dass Sie den Namen des übergeordneten Elements kennen, das die Elemente 'LoanDetails' und' ContributionFunds' enthält ('Data..' im angegebenen Beispiel). - PS Es ist nichts falsch daran, 'xsl: copy' zu verwenden. –

+0

@DanielCSD Wird Ihre Frage nicht beantwortet? –

0
<?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> 
Verwandte Themen