2015-03-13 26 views
0

這是我的XML文件:重複標籤的打印計數一次

<limitCheckApproved> 
<correlationId correlationIdScheme="tttt">SEF_correlationId</correlationId> 
<sequenceNumber>1</sequenceNumber> 
<party id="party1"> 
     <partyId>CLIENT1</partyId> 
    </party> 
    <party id="party2"> 
     <partyId>BARCGB2L</partyId> 
    </party> 
    <party id="clearingBroker1"> 
     <partyId>DB</partyId> 
    </party> 
    <party id="DCO"> 
     <partyId>LCH</partyId> 
    </party> 
    <party id="ExecutionFacility"> 
     <partyId>SEF1</partyId> 
    </party> 
</limitCheckApproved> 

這是我的XSLT文件:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:strip-space elements= "*"/> 
    <xsl:output indent="yes"/> 

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

     <xsl:template match="limitCheckApproved/correlationId"> 

     <FPCorID> 
      <xsl:attribute name = "FPCorIDSch"> 
      <xsl:value-of select = "correlationId/@correlationIdScheme"/> 
      </xsl:attribute> 
      <xsl:value-of select = "correlationId"/> 
     </FPCorID> 

     </xsl:template> 

     <xsl:template match = "limitCheckApproved/party"> 

     <FpNoOfPartyIDs> 
      <xsl:value-of select="count(/limitCheckApproved/party)"/> 
     </FpNoOfPartyIDs> 

     <FpPartyID> 
     <xsl:value-of select = "@id" /> 
     </FpPartyID> 

     <FpPartyIDValue> 
     <xsl:value-of select = "partyId" /> 
     </FpPartyIDValue> 

     </xsl:template> 

    </xsl:stylesheet> 

這是輸出:

<limitCheckApproved> 
<FPCorID correlationIdScheme="tttt">SEF_correlationId</FPCorID> 
<FpNoOfPartyIDs>5</FpNoOfPartyIDs> 
    <FpPartyID>party1</FpPartyID> 
    <FpPartyIDValue>CLIENT1</FpPartyIDValue> 
    <FpNoOfPartyIDs>5</FpNoOfPartyIDs> 
    <FpPartyID>clearingBroker1</FpPartyID> 
    <FpPartyIDValue>CM1</FpPartyIDValue> 
    <FpNoOfPartyIDs>5</FpNoOfPartyIDs> 
    <FpPartyID>LimitsHub</FpPartyID> 
    <FpPartyIDValue>Traiana</FpPartyIDValue> 
    <FpNoOfPartyIDs>5</FpNoOfPartyIDs> 
    <FpPartyID>ExecutionFacility</FpPartyID> 
    <FpPartyIDValue>SEF1</FpPartyIDValue> 
    <FpNoOfPartyIDs>5</FpNoOfPartyIDs> 
    <FpPartyID>DCO</FpPartyID> 
    <FpPartyIDValue>LCH</FpPartyIDValue> 
</limitCheckApproved> 

我需要什麼是FpNoOfPartyIDs標籤只打印一次,而其他標籤FpPartyIDValue和FpNoOfPartyIDs要打印正常。 但我找不到一個正確的方式來指定此。

我已經嘗試了幾個方法可以做到這一點:

. 
. 
. 
<xsl:if test = "partyId = 'party1'"> 
<FpNoOfPartyIDs> 
    <xsl:value-of select="count(/limitCheckApproved/party)"/> 
</FpNoOfPartyIDs> 
</xsl:if> 
. 
. 

但是這種方法以後會導致錯誤。

此外,我試圖做一個模板內的一切:

<xsl:template match="limitCheckApproved"> 
    <limitCheckApproved> 
    . 
    . 
    . 
    <FpNoOfPartyIDs> 
     <xsl:value-of select="count(/limitCheckApproved/party)"/> 
    </FpNoOfPartyIDs> 

    <xsl:for-each select = "party"> 

    <FpPartyID> 
    <xsl:value-of select = "@id" /> 
    </FpPartyID> 

    <FpPartyIDValue> 
    <xsl:value-of select = "partyId" /> 
    </FpPartyIDValue> 

    </xsl:for-each> 
    </limitCheckApproved> 
</xsl:template> 

但有一個更好的方式來做到這一點?

+0

http://stackoverflow.com/help/someone-answers – 2015-05-30 16:59:53

回答

0

首先,您向我們展示的輸出是而不是運行代碼時收到的輸出。具體而言,FPCorIDScha屬性爲空,sequenceNumber元素被複制到輸出。

我猜你想要做的事,如:

XSLT 1.0

<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="correlationId"> 
    <FPCorID FPCorIDSch="{@correlationIdScheme}"> 
     <xsl:value-of select="."/> 
    </FPCorID> 
    <FpNoOfPartyIDs> 
     <xsl:value-of select="count(../party)"/> 
    </FpNoOfPartyIDs> 
</xsl:template> 

<xsl:template match="party"> 
    <FpPartyID> 
     <xsl:value-of select="@id" /> 
    </FpPartyID> 
    <FpPartyIDValue> 
     <xsl:value-of select="partyId" /> 
    </FpPartyIDValue> 
</xsl:template> 

</xsl:stylesheet> 

這將產生這種結果

<?xml version="1.0" encoding="UTF-8"?> 
<limitCheckApproved> 
    <FPCorID FPCorIDSch="tttt">SEF_correlationId</FPCorID 
    <FpNoOfPartyIDs>5</FpNoOfPartyIDs> 
    <sequenceNumber>1</sequenceNumber> 
    <FpPartyID>party1</FpPartyID> 
    <FpPartyIDValue>CLIENT1</FpPartyIDValue> 
    <FpPartyID>party2</FpPartyID> 
    <FpPartyIDValue>BARCGB2L</FpPartyIDValue> 
    <FpPartyID>clearingBroker1</FpPartyID> 
    <FpPartyIDValue>DB</FpPartyIDValue> 
    <FpPartyID>DCO</FpPartyID> 
    <FpPartyIDValue>LCH</FpPartyIDValue> 
    <FpPartyID>ExecutionFacility</FpPartyID> 
    <FpPartyIDValue>SEF1</FpPartyIDValue> 
</limitCheckApproved> 

如果你不希望sequenceNumber被複制,添加另一個模板來抑制它:

<xsl:template match="sequenceNumber"/>