2010-04-23 28 views
0

在下面的XSL中,每當xsl:when得到滿足時,我都想要追加儘可能多的標籤和>和</a>標籤。但是需要在標籤內部填充的數據應該只有一次。我已經展示了最終的預期產出。 我該如何修改XSL以更改xml格式

<xsl:param name="insert-file" as="document-node()" /> 
<xsl:template match="*"> 
<xsl:variable name="input">My text</xsl:variable> 
<xsl:variable name="Myxml" as="element()*"> 
    <xsl:call-template name="populateTag"> 
      <xsl:with-param name="nodeValue" select="$input"/> 
    </xsl:call-template> 
</xsl:variable> 
<xsl:copy-of select="$Myxml"></xsl:copy-of> 
</xsl:template> 

<xsl:template name="populateTag"> 
    <xsl:param name="nodeValue"/> 
    <xsl:for-each select="$insert-file/insert-data/data"> 
     <xsl:choose> 
      <xsl:when test="@index = 1"> 
       <a><xsl:value-of select="$nodeValue"></xsl:value-of></a> 
      </xsl:when>    
     </xsl:choose> 
    </xsl:for-each> 
</xsl:template>  

電流輸出:

<?xml version="1.0" encoding="UTF-8"?> <a>我的文字</a> <a>我的文字</a> <a>我的文字</a> <a>我的文字</a>

一世 希望模板「populateTag」以下面的格式返回我的xml。我如何修改模板「populateTag」以達到相同效果。

從模板預期輸出 「populateTag」: <?xml version="1.0" encoding="UTF-8"?> <a><a><a><a>我的文字</a></a></a></a>

請給你的想法。

回答

1

要發生這種情況,您需要某種遞歸(嵌套a元素)。

沒有嘗試,因爲我沒有一個示例XML文檔:

<xsl:param name="insert-file" as="document-node()" /> 
<xsl:template match="*"> 
<xsl:variable name="input">My text</xsl:variable> 
<xsl:variable name="Myxml" as="element()*"> 
    <xsl:call-template name="populateTag"> 
      <xsl:with-param name="nodeValue" select="$input"/> 
      <xsl:with-param name="position" select="1"/> 
    </xsl:call-template> 
</xsl:variable> 
<xsl:copy-of select="$Myxml"></xsl:copy-of> 
</xsl:template> 

<xsl:template name="populateTag"> 
    <xsl:param name="nodeValue"/> 
    <xsl:param name="position"/> 
    <xsl:variable name="total" select="count($insert-file/insert-data/data[@index = 1])" /> 
    <xsl:for-each select="$insert-file/insert-data/data[@index = 1]"> 
     <xsl:if test="position() = $position" > 
      <xsl:choose> 
       <xsl:when test="position() = $total"> 
        <a><xsl:value-of select="$nodeValue"></xsl:value-of></a> 
       </xsl:when>    
       <xsl:otherwise> 
       <a>  
         <xsl:call-template name="populateTag"> 
           <xsl:with-param name="nodeValue" select="$input"/> 
           <xsl:with-param name="position" select="$position+1"/> 
         </xsl:call-template> 
       </a> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:if> 
    </xsl:for-each> 
</xsl:template> 
+0

感謝您的答覆。有用。 – user323719 2010-04-23 18:22:00