2014-07-25 51 views
0

我有這樣XSLT轉換和限制

<Ozellik isim="Renk-Beden">STANDART STD</Ozellik> 
<Ozellik isim="Grup">ADMIN</Ozellik> 
<Ozellik isim="Amac">DENEME</Ozellik> 
<Ozellik isim="BlaBla">BLABLA</Ozellik> 

的XML,並希望將其轉換爲這一點,並限制三個要素。如果有3分以上的記錄,把他們的前三個

<property1 name="Renk-Beden">STANDART STD</property1> 
<property2 name="Grup">ADMIN</property2> 
<property3 name="Amac">DENEME</property3> 

我試過很多XSLT代碼,但可能無法將其轉換爲所需的輸出。謝謝你的幫助。

我這是最後一次成功的嘗試是:

<xsl:template name="loop"> 
    <xsl:param name="pCount"/> 
    <xsl:param name="pValue"/> 
    <xsl:param name="pAtt"/> 
    <xsl:element name="property{$pCount}"> 
    <xsl:attribute name="name"> 
     <xsl:value-of select="$pAtt" /> 
    </xsl:attribute> 
     <xsl:value-of select="$pValue" /> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="Ozellik"> 
    <xsl:param name="pCount" select="0"/> 
    <xsl:for-each select="catalog/cd"> 
     <xsl:call-template name="loop"> 
     <xsl:with-param name="pCount" select="position()" /> 
     <xsl:with-param name="pValue" select="." /> 
     <xsl:with-param name="pAtt" select="@isim" /> 
     </xsl:call-template> 
    </xsl:for-each> 
    </xsl:template> 
+0

'我試過很多xslt代碼',可否請您發佈最後一次嘗試? –

+0

我添加了我的最後一次嘗試 – Can

+0

爲什麼你選擇'catalog/cd',沒有元素'catalog'或'cd'來自你輸入的樣本輸入 –

回答

1

使用

<xsl:template match="/"> 
    <xsl:apply-templates select="(//Ozellik)[position() &lt; 4]"/> 
</xsl:template> 

<xsl:template match="Ozellik"> 
    <xsl:element name="property{position()}"> 
    <xsl:apply-templates select="@* | node()"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="Ozellik/@isim"> 
    <xsl:attribute name="name"> 
    <xsl:value-of select="."/> 
    </xsl:attribute> 
</xsl:template> 
+0

謝謝你這個作品和下面的答案作品:) 哪一個應該標記? – Can

0

要解決你的最後一次嘗試。這是你需要做的。

卸下for-each元件

<xsl:for-each select="catalog/cd"> 
    <!-- existing inner elements --> 
</xsl:for-each> 

並用if元件替換它。

<xsl:if test="position() &lt; 4"> 
    <!-- existing inner elements --> 
</xsl:if> 

你的「Ozellik」模板看起來像這樣。

​​
+0

謝謝你這個工作 – Can