首页 > 解决方案 > 如何使用 XML 中的嵌套值填充 XSL 变量?

问题描述

我正在使用 XSL 1.0 并试图深入到一系列 XML 节点以检索一个值。这是我的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<mpcconfiguration>
   <lineitem id="0" seriesid="series1" modelid="model1">
      <seriesdesc>Series #1</seriesdesc>
      <modeldesc>Model #1</modeldesc>
      <category id="Mstr_Information">
         <option id="Mstr_Information">
            <unitprice>0</unitprice>
            <property id="ExtPrice">0</property>
            <property id="Mstr_ModelSortOrder">3</property>
         </option>
      </category>
   </lineitem>
   <lineitem id="1" seriesid="series2" modelid="model2">
      <seriesdesc>Series #2</seriesdesc>
      <modeldesc>Model #2</modeldesc>
      <category id="Mstr_Information">
         <option id="Mstr_Information">
            <description>descr1</description>
            <unitprice>0</unitprice>
            <property id="ExtPrice">0</property>
            <property id="Mstr_ModelSortOrder">1</property>
         </option>
      </category>
   </lineitem>
   <lineitem id="2" seriesid="series3" modelid="model3">
      <seriesdesc>Series #3</seriesdesc>
      <modeldesc>Model #3</modeldesc>
      <category id="Mstr_Information">
         <option id="Mstr_Information">
            <description>descr1</description>
            <unitprice>0</unitprice>
            <property id="ExtPrice">0</property>
            <property id="Mstr_ModelSortOrder">2</property>
         </option>
      </category>
   </lineitem>
</mpcconfiguration>

<lineitem>这是我要获取的每个值的选择器:

category[@id='Mstr_Information']/option[@id='Mstr_Information']/property[@id='Mstr_ModelSortOrder']

我知道上面的选择器是正确的,因为我成功地将它用于 XSL 顶部附近的排序目的。(谢谢托马拉克!)

我正试图将其提取到一个变量中:

<xsl:variable name="modelSortOrder">
    <xsl:value-of select="category[@id='Mstr_Information']/option[@id='Mstr_Information']/property[@id='Mstr_ModelSortOrder']"/>
</xsl:variable>

...然后像这样稍后使用它:

<span style="width: 50px">
   <b>Model Sort Order: </b>
</span>
<xsl:value-of select="$modelSortOrder" />

失败:我最终得到一个空字符串$modelSortOrder

这是我当前 XSL 的精简版(希望可以运行):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ms="urn:schemas-microsoft-com:xslt">
   
   <xsl:template match="mpcconfiguration">
      <xsl:copy>
         <xsl:apply-templates select="@*" />
         <xsl:apply-templates select="lineitem">
            <xsl:sort select="category[@id='Mstr_Information']/option[@id='Mstr_Information']/property[@id='Mstr_ModelSortOrder']" data-type="number" />
         </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>
   
   <xsl:template match="lineitem">
      <xsl:variable name="lineItemId">
         <xsl:value-of select="@id"/>
      </xsl:variable>
      <xsl:variable name="seriesId">
         <xsl:value-of select="@seriesid"/>
      </xsl:variable>
      <xsl:variable name="modelId">
         <xsl:value-of select="@modelid"/>
      </xsl:variable>
      
      <xsl:variable name="modelSortOrder">
         <xsl:value-of select="category[@id='Mstr_Information']/option[@id='Mstr_Information']/property[@id='Mstr_ModelSortOrder']"/>
      </xsl:variable>

      <div class="xRgn">
         <table class="xInner">
            <tr>
               <td class="col">
                  <xsl:element name="div">
                     <div style="float:left; margin:3px">
                        
                      <span style="width: 50px">
                         <b>Series: </b>
                      </span>
                      <xsl:choose>
                         <xsl:when test="not(seriesdesc)">
                            <xsl:value-of select="@seriesid" />
                         </xsl:when>
                         <xsl:otherwise>
                            <xsl:value-of select="seriesdesc" />
                         </xsl:otherwise>
                      </xsl:choose>
                      <br />
                      <span style="width: 50px">
                         <b>Model: </b>
                      </span>
                      <xsl:choose>
                         <xsl:when test="not(modeldesc)">
                            <xsl:value-of select="@modelid" />
                         </xsl:when>
                         <xsl:otherwise>
                            <xsl:value-of select="modeldesc" />
                         </xsl:otherwise>
                      </xsl:choose>
                      <br />
                      <span style="width: 50px">
                         <b>Model Sort Order: </b>
                      </span>
                      <xsl:value-of select="$modelSortOrder" />
                      <br />
                      
                     </div>
                  </xsl:element>
               </td>
            </tr>
         </table>
      </div>
   </xsl:template>
</xsl:stylesheet>

以上在我的 .NET 环境中运行良好(空变量除外),但希望它也能在您使用的任何工具中为您运行。我的 XSLT 输出正确排序,正确循环各个<lineitem>部分,并为每个部分正确发出 HTML。

标签: xmlxsltxslt-1.0

解决方案


推荐阅读