2017-11-03 8 views
1

属性を動的に設定しようとしていますが、何もできません。私たちを手伝ってくれますか?望ましい例:)属性値を動的に増やします。XSLT

入力:

<root> 
<row> 
    <col>v11</col> 
    <col>v12</col> 
    <col>v13</col> 
    <col>v14</col> 
    </row> 
    <row> 
    <col>v21</col> 
    <col>v22</col> 
    <col>v23</col> 
    <col>v24</col> 
    </row> 
</root> 

現在のXSLTスキーム:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" encoding="utf-8" indent="yes"/> 
    <xsl:template match="root"> 
     <root> 
      <xsl:apply-templates select="row"/> 
     </root> 
    </xsl:template> 

    <xsl:template match="col"> 
     <data col="1" row="1"> 
      <xsl:value-of select="."/> 
     </data> 
    </xsl:template> 

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

</xsl:stylesheet> 

電流出力:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <data col="1" row="1">v11</data> 
    <data col="1" row="1">v12</data> 
    <data col="1" row="1">v13</data> 
    <data col="1" row="1">v14</data> 
    <data col="1" row="1">v21</data> 
    <data col="1" row="1">v22</data> 
    <data col="1" row="1">v23</data> 
    <data col="1" row="1">v24</data> 
</root> 

どのように私は動的に属性値を割り当てることができますか?

私はそれを行う必要があります。

<root> 
    <data row="1" col="1">v11</data> 
    <data row="1" col="2">v12</data> 
    <data row="1" col="3">v13</data> 
    <data row="1" col="4">v14</data> 
    <data row="2" col="1">v21</data> 
    <data row="2" col="2">v22</data> 
    <data row="2" col="3">v23</data> 
    <data row="2" col="4">v24</data> 
</root> 

ありがとう!

答えて

0

あなたがの位置と一緒に、このパラメータを使用することができますrowテンプレートマッチングでは、次にrow ...

<xsl:apply-templates select="col"> 
    <xsl:with-param name="row" select="position()" /> 
</xsl:apply-templates> 

のpostionに合格するcol要素のapply-templatesを改正した場合col要素

<data col="{position()}" row="{$row}"> 
    <xsl:value-of select="."/> 
</data> 

は、属性を作成するために、ここで中括弧の使用に注意してください。これらはAttribute Value Templates

として知られているほかにposition()を使用してから、このXSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" encoding="utf-8" indent="yes"/> 
    <xsl:template match="root"> 
     <root> 
      <xsl:apply-templates select="row"/> 
     </root> 
    </xsl:template> 

    <xsl:template match="col"> 
     <xsl:param name="row" /> 
     <data col="{position()}" row="{$row}"> 
      <xsl:value-of select="."/> 
     </data> 
    </xsl:template> 

    <xsl:template match="row"> 
    <xsl:apply-templates select="col"> 
     <xsl:with-param name="row" select="position()" /> 
    </xsl:apply-templates> 
    </xsl:template> 
</xsl:stylesheet> 
+0

これはどのように動作するのですか? ありがとう! –

2

を試してみて、これを達成するための別の方法はxsl:numberを使用することです:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" encoding="utf-8" indent="yes"/> 
    <xsl:template match="root"> 
    <root> 
     <xsl:apply-templates select="row"/> 
    </root> 
    </xsl:template> 

    <xsl:template match="col"> 
    <data> 
     <xsl:attribute name="row"> 
     <xsl:number count="row" /> 
     </xsl:attribute> 
     <xsl:attribute name="col"> 
     <xsl:number count="col" /> 
     </xsl:attribute> 
     <xsl:value-of select="."/> 
    </data> 
    </xsl:template> 

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

</xsl:stylesheet> 

これはあなたのサンプル入力で実行されると、結果は次のとおりです。

<root> 
    <data row="1" col="1">v11</data> 
    <data row="1" col="2">v12</data> 
    <data row="1" col="3">v13</data> 
    <data row="1" col="4">v14</data> 
    <data row="2" col="1">v21</data> 
    <data row="2" col="2">v22</data> 
    <data row="2" col="3">v23</data> 
    <data row="2" col="4">v24</data> 
</root> 
0
<xsl:template match="col"> 
    <data col="{count(preceding-sibling::col)+1}" 
      row="{count(../preceding-sibling::row)+1}"> 
     <xsl:value-of select="."/> 
    </data> 
</xsl:template> 

何すべてのこれらのソリューションは共通していることは、彼らがプログラム変数に変更可能な状態を維持することなく、入力の関数として出力を計算することである:210そして、ここでは、第三の方法です。それが関数型プログラミングの本質です。

関連する問題