使用模板匹配和变量名称的XSLT混淆

时间:2016-02-16 03:11:00

标签: xml xslt

https://gist.github.com/anonymous/1afe3959274d49233854

我正在尝试将我正在学习的xml文件转换为html表,并且到目前为止我做的还不错,但是将元素链接回到id有点困惑。我在上面的gist链接中包含了代码。打败我的头,但是每次都变得不同,但结果不正确。有谁看到我错过了什么?

1 个答案:

答案 0 :(得分:0)

你可以在第一次循环后立即声明你的变量

<xsl:for-each select="dronecollection/drones/dronetype">
    <xsl:variable name="sizeid" select="@s_id"/>
    ...

然后选择与目标节点的s_id匹配的值,例如:

<xsl:value-of select="ancestor::drones/following-sibling::modelsize/size[@s_id=$sizeid]/description"/>

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--<?xml-stylesheet type="text/xsl" href="drones_list.xsl" ?>-->
<?xml-stylesheet type="text/xsl" href="drones_tabular.xsl" ?>
<dronecollection>
    <drones>
        <dronetype d_id="_1" t_id="_01" s_id="_001">
            <model_name>Phantom 3</model_name>
            <price>699</price>
            <flighttime_min>17</flighttime_min>
            <flighttime_max>20</flighttime_max>
        </dronetype>

        <dronetype d_id="_2" t_id="_01" s_id="_002">
            <model_name>Inspire 1</model_name>
            <price>2899</price>
            <flighttime_min>18</flighttime_min>
            <flighttime_max>20</flighttime_max>

        </dronetype>

        <dronetype d_id="_3" t_id="_02" s_id="_001">
            <model_name>Q500 4K</model_name>
            <price>1300</price>
            <flighttime_min>20</flighttime_min>
            <flighttime_max>25</flighttime_max>
        </dronetype>

        <dronetype d_id="_4" t_id="_03" s_id="_001">
            <model_name>Solo</model_name>
            <price>999</price>
            <flighttime_min>20</flighttime_min>
            <flighttime_max>25</flighttime_max>
        </dronetype>

        <dronetype d_id="_5" t_id="_04" s_id="_004">
            <model_name>X4</model_name>
            <price>45</price>
            <flighttime_min>5</flighttime_min>
            <flighttime_max>7</flighttime_max>
        </dronetype>

        <dronetype d_id="_6" t_id="_05" s_id="_004">
            <model_name>Blade Nano QX</model_name>
            <price>90</price>
            <flighttime_min>5</flighttime_min>
            <flighttime_max>7</flighttime_max>
        </dronetype>

        <dronetype d_id="_7" t_id="_06" s_id="_003">
            <model_name>Bebop</model_name>
            <price>499</price>
            <flighttime_min>8</flighttime_min>
            <flighttime_max>10</flighttime_max>
        </dronetype>

        <dronetype d_id="_8" t_id="_07" s_id="_003">
            <model_name>TBS GEMINI</model_name>
            <price>600</price>
            <flighttime_min>5</flighttime_min>
            <flighttime_max>8</flighttime_max>
        </dronetype>

        <dronetype d_id="_9" t_id="_03" s_id="_001">
            <model_name>IRIS</model_name>
            <price>740</price>
            <flighttime_min>10</flighttime_min>
            <flighttime_max>15</flighttime_max>
        </dronetype>

        <dronetype d_id="_10" t_id="_03" s_id="_002">
            <model_name>X8</model_name>
            <price>1300</price>
            <flighttime_min>12</flighttime_min>
            <flighttime_max>17</flighttime_max>
        </dronetype>

    </drones>

    <manufacturer_type>
        <type t_id="_01">
            <manufacturer>DJI</manufacturer>
        </type>
        <type t_id="_02">
            <manufacturer>Yuneec</manufacturer>
        </type>
        <type t_id="_03">
            <manufacturer>3DR Robotics</manufacturer>
        </type>
        <type t_id="_04">
            <manufacturer>Hubsan</manufacturer>
        </type>
        <type t_id="_05">
            <manufacturer>Horizon Hobby</manufacturer>
        </type>
        <type t_id="_06">
            <manufacturer>Parrot</manufacturer>
        </type>
        <type t_id="_07">
            <manufacturer>Team BlackSheep</manufacturer>
        </type>
    </manufacturer_type>

    <modelsize>
        <size s_id="_001">
            <description>Medium</description>
        </size>
        <size s_id="_002">
            <description>Large</description>
        </size>
        <size s_id="_003">
            <description>Small</description>
        </size>
        <size s_id="_004">
            <description>Very Small</description>
        </size>
    </modelsize>
</dronecollection>

和这个样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <html>
            <body>
                <h2>XSLT Transformation</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Manufacturer</th>
                        <th>Model Name</th>
                        <th>Estimated Price ($)</th>
                        <th>Flight Time (min)</th>
                        <th>Model Size</th>
                    </tr>
                    <xsl:for-each select="dronecollection/drones/dronetype">
                        <xsl:variable name="sizeid" select="@s_id"/>
                        <tr>
                            <td>
                                <xsl:apply-templates select="model_name"/>
                            </td>
                            <td>
                                <xsl:value-of select="model_name"/>
                            </td>
                            <td>
                                <xsl:value-of select="price"/>
                            </td>
                            <td>
                                <xsl:value-of select="flighttime_min"/>
                                <xsl:text> to </xsl:text>
                                <xsl:value-of select="flighttime_max"/>
                            </td>
                            <td>
                                <xsl:value-of select="ancestor::drones/following-sibling::modelsize/size[@s_id=$sizeid]/description"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

输出

<html>
   <body>
      <h2>XSLT Transformation</h2>
      <table border="1">
         <tr bgcolor="#9acd32">
            <th>Manufacturer</th>
            <th>Model Name</th>
            <th>Estimated Price ($)</th>
            <th>Flight Time (min)</th>
            <th>Model Size</th>
         </tr>
         <tr>
            <td>Phantom 3</td>
            <td>Phantom 3</td>
            <td>699</td>
            <td>17 to 20</td>
            <td>Medium</td>
         </tr>
         <tr>
            <td>Inspire 1</td>
            <td>Inspire 1</td>
            <td>2899</td>
            <td>18 to 20</td>
            <td>Large</td>
         </tr>
         <tr>
            <td>Q500 4K</td>
            <td>Q500 4K</td>
            <td>1300</td>
            <td>20 to 25</td>
            <td>Medium</td>
         </tr>
         <tr>
            <td>Solo</td>
            <td>Solo</td>
            <td>999</td>
            <td>20 to 25</td>
            <td>Medium</td>
         </tr>
         <tr>
            <td>X4</td>
            <td>X4</td>
            <td>45</td>
            <td>5 to 7</td>
            <td>Very Small</td>
         </tr>
         <tr>
            <td>Blade Nano QX</td>
            <td>Blade Nano QX</td>
            <td>90</td>
            <td>5 to 7</td>
            <td>Very Small</td>
         </tr>
         <tr>
            <td>Bebop</td>
            <td>Bebop</td>
            <td>499</td>
            <td>8 to 10</td>
            <td>Small</td>
         </tr>
         <tr>
            <td>TBS GEMINI</td>
            <td>TBS GEMINI</td>
            <td>600</td>
            <td>5 to 8</td>
            <td>Small</td>
         </tr>
         <tr>
            <td>IRIS</td>
            <td>IRIS</td>
            <td>740</td>
            <td>10 to 15</td>
            <td>Medium</td>
         </tr>
         <tr>
            <td>X8</td>
            <td>X8</td>
            <td>1300</td>
            <td>12 to 17</td>
            <td>Large</td>
         </tr>
      </table>
   </body>
</html>