0

私はxmlファイルを持っています

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="test1.xsl"?>
<products>
    <node>
        <node>
            <dist_value>
            <node> 55 </node>
            <node> 59 </node>
            <node> 72 </node>
            </dist_value>
            <reg_str_dt>
            <node> 2013-08-03 17:29:00 </node>
            </reg_str_dt>
            <product_id> 1 </product_id>
        </node>
    </node>
    <node>
        <node>
            <dist_value>
            <node> 72 </node>
            <node> 19 </node>
            <node> 49 </node>
            </dist_value>
            <reg_str_dt>
            <node> 2013-10-25 17:29:00 </node>
            </reg_str_dt>
            <product_id> 2 </product_id>
        </node>
    </node> 
    <node>
        <node>
            <dist_value>
            <node> 12 </node>
            <node> 548 </node>
            <node> 112 </node>
            </dist_value>
            <reg_str_dt>
            <node> 2013-08-12 17:29:00 </node>
            </reg_str_dt>
            <name> test </name>
            <product_id> 3 </product_id>
        </node>
    </node>
</products>

私はこのxsltを書いて、製品のすべてのデータを表示し、製品dist_value\node < 50の出力データを提供します2 & 3

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="/">
   <html>
    <body>

     <table border="1">
      <tr><th>Product ID</th><th>Product DATA</th></tr>
      <xsl:apply-templates select="products/node/node" />
     </table>
    </body>
   </html>
  </xsl:template>

  <xsl:template match="products/node/node/dist_value[node &lt; 50]">
   <tr>
    <td><xsl:value-of select="//product_id" /></td>
    <td><xsl:value-of select="products/node/node/*" /></td> 
   </tr>
  </xsl:template> 

</xsl:stylesheet>

私はxsltの初心者ですが、ここで何かが間違っています

私はこのようなものを出力したい

Product ID  |   Product DATA
--------------------------------
2           |dist_value  => 72
            |               19
            |               79
            |reg_str_dt =>  2013-10-25 17:29:00
            |product_id => 2

製品 3 に名前が含まれている

4

4 に答える 4

1

テンプレートのselectステートメントを少し変更する必要があります。一致するステートメントではノードを選択しているため、テンプレートの残りの部分では、次のように、selectステートメントはそのノードに<dist_value>相対的である必要があります。<dist_value>

<xsl:template match="products/node/node/dist_value[node &lt; 50]">
  <tr>
    <td>
      <xsl:value-of select="../product_id" />
    </td>
    <td>
      <xsl:for-each select="./node">
        <xsl:value-of select="."/>
        <br/>
      </xsl:for-each>
      <xsl:value-of select="../reg_str_dt/node" />
    </td>
  </tr>
</xsl:template>
于 2013-02-27T08:20:05.167 に答える
1

XSLT について知っておくべきことの 1 つは、組み込みテンプレートの概念があることです。これらのテンプレートは、要素に一致するテンプレートを探しているが、XSLT に存在しない場合に使用されます。あなたの場合、ノード要素を探すことから始めます

<xsl:apply-templates select="products/node/node" />

ただし、テンプレートはdist_value要素と一致しています

<xsl:template match="products/node/node/dist_value[node &lt; 50]">

これは、XSLT が組み込みテンプレートの使用を開始し、要素のテキストを出力してから子を処理することを意味します。ノード要素に一致させるには、おそらくこのようなことを行う必要があります。

<xsl:template match="products/node/node[dist_value/node &lt; 50]">

dist_valueが 50 以上のノード要素に一致するテンプレートも必要ですが、必要な要素だけを選択するように apply-templates を変更することもできます。

<xsl:apply-templates select="products/node/node[dist_value/node &lt; 50]" />

あなたが持っている別の問題は、dist_valueテンプレート内のこの行にあります

<xsl:value-of select="//product_id" />

二重スラッシュは、ルート要素に相対的なproduct_idを探し、常に最初のものを取得することを意味します。現在のノード要素に相対的なproduct_idを探すために、本当にこれを行う必要があります

<xsl:value-of select="product_id" />

ここに完全な XSLT があります

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>

   <xsl:template match="/">
      <html>
         <body>
            <table border="1">
               <tr>
                  <th>Product ID</th>
                  <th>Product DATA</th>
               </tr>
               <xsl:apply-templates select="products/node/node[dist_value/node &lt; 50]"/>
            </table>
         </body>
      </html>
   </xsl:template>

   <xsl:template match="products/node/node">
      <tr>
         <td>
            <xsl:value-of select="product_id"/>
         </td>
         <td>
            <xsl:apply-templates select="dist_value/node"/>
         </td>
      </tr>
      <tr>
         <td/>
         <td>
            <xsl:value-of select="reg_str_dt/node"/>
         </td>
      </tr>
      <tr>
         <td/>
         <td>
            <xsl:value-of select="product_id"/>
         </td>
      </tr>
   </xsl:template>

   <xsl:template match="dist_value/node">
      <xsl:value-of select="concat(., '&#10;')"/>
   </xsl:template>
</xsl:stylesheet>

XML に適用すると、以下が出力されます。

<html>
   <body>
      <table border="1">
         <tr>
            <th>Product ID</th>
            <th>Product DATA</th>
         </tr>
         <tr>
            <td> 2 </td>
            <td> 72 19 49 </td>
         </tr>
         <tr>
            <td/>
            <td> 2013-10-25 17:29:00 </td>
         </tr>
         <tr>
            <td/>
            <td> 2 </td>
         </tr>
         <tr>
            <td> 3 </td>
            <td> 12 548 112 </td>
         </tr>
         <tr>
            <td/>
            <td> 2013-08-12 17:29:00 </td>
         </tr>
         <tr>
            <td/>
            <td> 3 </td>
         </tr>
      </table>
   </body>
</html>
于 2013-02-27T08:31:48.463 に答える
1

に表示できるさまざまな要素に関しては、もう少し柔軟な別のオプションを次に示します/products/node/nodenodeの子内の要素の数についても、より柔軟です/products/node/node

XML 入力(ディスプレイを表示するためにいくつかの追加のテスト値を表示するように変更されています)

<products>
    <node>
        <node>
            <dist_value>
                <node> 55 </node>
                <node> 59 </node>
                <node> 72 </node>
            </dist_value>
            <reg_str_dt>
                <node> 2013-08-03 17:29:00 </node>
            </reg_str_dt>
            <product_id> 1 </product_id>
        </node>
    </node>
    <node>
        <node>
            <dist_value>
                <node> 72 </node>
                <node> 19 </node>
                <node> 49 </node>
            </dist_value>
            <reg_str_dt>
                <node> 2013-10-25 17:29:00 </node>
                <node>additional test value 1</node>
                <node>additional test value 2</node>
            </reg_str_dt>
            <product_id> 2 </product_id>
        </node>
    </node> 
    <node>
        <node>
            <dist_value>
                <node> 12 </node>
                <node> 548 </node>
                <node> 112 </node>
            </dist_value>
            <reg_str_dt>
                <node> 2013-08-12 17:29:00 </node>
            </reg_str_dt>
            <name> test </name>
            <product_id> 3 </product_id>
        </node>
    </node>
</products>

XSLT1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/products" priority="1">
        <html>
            <body>
                <table border="1">
                    <tr>
                        <th>Product ID</th>
                        <th colspan="2">Product DATA</th>
                    </tr>
                    <xsl:apply-templates select="node[node/dist_value/node &lt; 50]"/>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="node/node">
        <tr>
            <td>
                <xsl:value-of select="product_id"/>
            </td>
            <td><xsl:value-of select="name(*[1])"/> --></td> 
            <td><xsl:value-of select="*[1]/*[1]"/></td>
        </tr>
        <xsl:apply-templates select="*[not(self::product_id)]"/>
    </xsl:template>

    <!--These 2 templates will handle the data on the same
    row as the Product ID.-->
    <xsl:template match="node/node/*[1]" priority="1">
        <xsl:apply-templates mode="newrow"/>
    </xsl:template>
    <xsl:template match="node/node/*[1]/*[1]" mode="newrow"/>

    <xsl:template match="*" mode="newrow">
        <xsl:call-template name="dataRow"/>
    </xsl:template>

    <xsl:template match="node/node/*/*[not(position()=1)]">
        <xsl:call-template name="dataRow"/>
    </xsl:template>

    <xsl:template name="dataRow">
        <tr>
            <td/>
            <td/>
            <td><xsl:value-of select="."/></td>
        </tr>       
    </xsl:template>

    <xsl:template match="*[not(self::node)]">
        <tr>
            <td/>
            <td><xsl:value-of select="name()"/> --></td>
            <td><xsl:apply-templates/></td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

HTML 出力(コード)

<html>
   <body>
      <table border="1">
         <tr>
            <th>Product ID</th>
            <th colspan="2">Product DATA</th>
         </tr>
         <tr>
            <td> 2 </td>
            <td>dist_value --&gt;</td>
            <td> 72 </td>
         </tr>
         <tr>
            <td></td>
            <td></td>
            <td> 19 </td>
         </tr>
         <tr>
            <td></td>
            <td></td>
            <td> 49 </td>
         </tr>
         <tr>
            <td></td>
            <td>reg_str_dt --&gt;</td>
            <td> 2013-10-25 17:29:00 
               <tr>
                  <td></td>
                  <td></td>
                  <td>additional test value 1</td>
               </tr>
               <tr>
                  <td></td>
                  <td></td>
                  <td>additional test value 2</td>
               </tr>
            </td>
         </tr>
         <tr>
            <td> 3 </td>
            <td>dist_value --&gt;</td>
            <td> 12 </td>
         </tr>
         <tr>
            <td></td>
            <td></td>
            <td> 548 </td>
         </tr>
         <tr>
            <td></td>
            <td></td>
            <td> 112 </td>
         </tr>
         <tr>
            <td></td>
            <td>reg_str_dt --&gt;</td>
            <td> 2013-08-12 17:29:00 </td>
         </tr>
         <tr>
            <td></td>
            <td>name --&gt;</td>
            <td> test </td>
         </tr>
      </table>
   </body>
</html>

HTML 出力(IE で表示)

ここに画像の説明を入力

于 2013-02-27T08:55:18.963 に答える
1

あなたのコードにはかなりの数の問題がありました。コンテキスト ノード、テンプレートの適用、およびプロセッサがノードを反復処理する方法について学習することに集中してください。また、あなたのスキーマは厄介です。可能であれば、「ノード/ノード」を「製品」に置き換えることを検討してください。

XSL は次のとおりです。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>

<xsl:template match="/">
    <html>
        <body>
            <table border="1">
                <tr><th>Product ID</th><th>Product DATA</th></tr>
                <xsl:apply-templates select="/products/node/node" />
            </table>
        </body>
    </html>
</xsl:template>

<!-- ignore the node with dist_value child by default -->
<xsl:template match="node[ dist_value ]" />
<xsl:template match="node[ dist_value[node &lt; 50] ]" priority="1.0">
    <xsl:variable name="span" select="1 + count( * )" />
    <tr>
        <td align="center" rowspan="{$span}"><xsl:value-of select="product_id" /></td>
    </tr>
    <xsl:apply-templates />
</xsl:template> 

<xsl:template match="name">
    <tr>
        <td>
            <xsl:value-of select="name()" />
            <xsl:text disable-output-escaping="yes"> => </xsl:text>
            <xsl:value-of select="." />
        </td>
    </tr>
</xsl:template>

<xsl:template match="dist_value">
    <tr>
        <td>
            <xsl:value-of select="name()"/>
            <xsl:text disable-output-escaping="yes"> =&gt; </xsl:text>
            <xsl:for-each select="*">
                <xsl:if test="position()>1">
                    <xsl:text>, </xsl:text>
                </xsl:if>
                <xsl:value-of select="normalize-space(.)" />
            </xsl:for-each> 
        </td>
    </tr>
</xsl:template>

<xsl:template match="reg_str_dt">
    <tr>
        <td>
            <xsl:value-of select="name()"/>
            <xsl:text disable-output-escaping="yes"> =&gt; </xsl:text>
            <xsl:value-of select="node" />
        </td>
    </tr>
</xsl:template>

<xsl:template match="product_id">
    <tr>
        <td>
            <xsl:value-of select="name()"/>
            <xsl:text disable-output-escaping="yes"> =&gt; </xsl:text>
            <xsl:value-of select="." />
        </td>
    </tr>
</xsl:template>

出力 HTML は次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<html>
   <body>
      <table border="1">
         <tr>
            <th>Product ID</th>
            <th>Product DATA</th>
         </tr>
         <tr>
            <td align="center" rowspan="4">2</td>
         </tr>
         <tr>
            <td>dist_value =&gt; 72, 19, 49</td>
         </tr>
         <tr>
            <td>reg_str_dt =&gt;  2013-10-25 17:29:00</td>
         </tr>
         <tr>
            <td>product_id =&gt;  2</td>
         </tr>
         <tr>
            <td align="center" rowspan="5">3</td>
         </tr>
         <tr>
            <td>dist_value =&gt; 12, 548, 112</td>
         </tr>
         <tr>
            <td>reg_str_dt =&gt;  2013-08-12 17:29:00</td>
         </tr>
         <tr>
            <td>name =&gt;  test</td>
         </tr>
         <tr>
            <td>product_id =&gt;  3</td>
         </tr>
      </table>
   </body>
</html>
于 2013-02-27T08:36:32.530 に答える