2012-08-09 62 views
0

我在XSLT中超級生鏽,並在想如果有人能給我一些指示。在XSLT中計算節點並複製時沒有值

編輯:使用XSLT 1.0

原始XML:

<gic> 
    <application> 
     <agent> 
      ...child nodes 
     </agent> 
     <client> 
      ...child nodes 
     </client> 
     <bank> 
      ...child nodes 
     </bank> 
    </application> 
</gic> 

我需要改變給定的XML INPUT有5客戶端節點。輸入可以包含1-5個客戶端節點。我需要確保輸出中總是有5個。在這種情況下,一個是提供,所以我需要插入4個客戶端節點與所有子節點。所有子節點的值都需要爲空。以XML輸出

回答

0
<?xml version="1.0" encoding="UTF-8"?> 

<xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"  
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:apply-templates /> 
</xsl:template> 


<xsl:template match="application" as="node()*"> 
    <xsl:copy> 
    <xsl:apply-templates select="agent" />  
    <!-- copy existing cliens --> 
    <xsl:apply-templates select="client" />  
    <!-- add new clients --> 
    <xsl:call-template name="AddClients"> 
     <xsl:with-param name="times" select="10 - count(client)" /> 
    </xsl:call-template> 

    <!-- copy banks --> 
    <xsl:apply-templates select="bank" />    
    </xsl:copy> 
</xsl:template> 


<xsl:template name="AddClients"> 
    <xsl:param name="times" select="1" /> 
    <xsl:if test="number($times) &gt; 0">  
    <!-- new element here --> 
    <xsl:element name="client"> 
     <xsl:attribute name="a1"> 
     <xsl:value-of select="asas" /> 
     </xsl:attribute> 
    </xsl:element> 

    <xsl:call-template name="AddClients"> 
     <xsl:with-param name="times" select="$times - 1" /> 
    </xsl:call-template> 
    </xsl:if> 
</xsl:template> 

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

</xsl:stylesheet> 
+0

感謝您的回答@Lesiak。我編輯了我的問題來簡化並希望更清楚一點。欣賞你的時間。 – GJones 2012-08-17 19:30:00