这是我的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 :(得分: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"/>