繁体   English   中英

如何在xsl:for-each中使用变量,但稍后再创建输出?

[英]How to use variables in xsl:for-each but create the output later?

我正在努力使输出按要求显示。

我运行一个xslt 1.0文件,该文件每5秒分析一个包含统计信息的xml文件。 每隔15分钟,我们就会获得一个额外的文件进行解析,该文件会作为外部源被提取。 解析很好,但是显示信息失败了。 这是创建输出的部分:

<td>
  <table cellSpacing="0" border="1" cellPadding="2" style="background-color:lightblue;color:green;font-size:80%;">
    <xsl:for-each select="/DialStats/Countries">
      <!-- @Country = 15 min 2nd XML file / $Country = 5 sec prim XML file-->
      <xsl:if test="@Country = $Country">
          <xsl:for-each select="./Products">
            <!-- @Product = 15 min 2nd XML file / $Product = 5 sec prim XML file-->
            <xsl:if test="@Product = $Product">
              <xsl:for-each select="Dispositions">
                <xsl:variable name="TSB">
                  <xsl:if test="@Result = 'Tour / Sale / Booking'">
                    <xsl:value-of select="@Count"/>
                   </xsl:if>
                 </xsl:variable>
<!-- // set multiple variables:  -->
                 <xsl:variable name="ITF">
                   <xsl:if test="@Result = 'Internal Transfer'">
                     <xsl:value-of select="@Count"/>
                   </xsl:if>
                 </xsl:variable>

<!-- // Now generate the output:  -->
                 <xsl:choose>
                   <xsl:when test="string-length($TSB)=0 and string-length($NoAgent)=0">
                     <!-- Don't do anything -->
                   </xsl:when>  
                   <xsl:when test="string-length($TSB)!=0 and string-length($NoAgent)=0">
                     <tr>
                       <td>
                         <xsl:value-of select="$TSB"/>
                       </td>
                       <td> - </td>
                       <td>TSB</td>
                       <td>. .</td>
                       <td colspan="3"></td>
                     </tr>
                   </xsl:when>
                   <xsl:when test="string-length($TSB)=0 and string-length($NoAgent)!=0">
                     <tr>
                       <td colspan="3"></td>
                       <td>. .</td>
                       <td>
                         <xsl:value-of select="$NoAgent"/>
                       </td>
                       <td>-</td>
                       <td>No Agent</td>
                     </tr>
                   </xsl:when>
                   <xsl:otherwise>
                     <tr>
                       <td>
                         <xsl:value-of select="$TSB"/>
                       </td>
                       <td> - </td>
                       <td>TSB</td>
                       <td>. .</td>
                       <td>
                         <xsl:value-of select="$NoAgent"/>
                       </td>
                       <td>-</td>
                       <td>No Agent</td>
                     </tr>
                   </xsl:otherwise>
                 </xsl:choose>
<!-- // Generate more of the output:  -->
<!-- // Last section  -->
                  <xsl:choose>
                    <xsl:when test="string-length($OTH)=0">
                      <!-- Don't do anything -->
                    </xsl:when>
                    <xsl:when test="string-length($OTH)!=0">
                      <tr>
                        <td>
                          <xsl:value-of select="$OTH"/>
                        </td>
                        <td>-</td>
                        <td>OTH</td>
                        <td>. .</td>
                        <td colspan="3"></td>
                      </tr>
                    </xsl:when>
                    <xsl:otherwise>
                      <!-- Nothing to do -->
                    </xsl:otherwise>
                  </xsl:choose>
               </xsl:for-each>
             </xsl:if>
           </xsl:for-each>
         </xsl:if>
       </xsl:for-each>
   </table>
 </td>

这会生成一个表,但是由于XML文件包含的信息的方式,该表并非始终位于同一位置,因此可能根本不在该位置。 我希望这些信息在不同位置显示在同一位置。

这就是我想要的显示方式

x - TSB  | x - NoAgent
  x - OSA  | x - LO
  x - IP   | x - UEE
  x - MES  | x - SIT
  x - NoVM | x - RNA
  x - NSR  | x - ITF
  x - OTH

如果值为0,那么我都将信息留空,如果两者都为空,那么我想把整行都留在外面。

这就是它的显示方式

9 - MES  |   
3 - NoVM |  
7 - IP   |  
         | 5 - LO 
6 - OTH  |  
         | 6 - RNA 
         | 4 - SIT 
3 -  TSB |

有没有办法在创建输出之前先创建所有变量? 我尝试过,但是最终我遇到了变量超出范围的问题。

期待建议

XML如下所示:

 <?xml version="1.0" encoding="utf-8"?>
 <DialStats>
    <Countries Country="France">
     <Products Product="Office">
       <Dispositions Result="Agent Voicemail / Message" Count="11" />
       <Dispositions Result="Did Not Leave Voicemail" Count="37" />
       <Dispositions Result="Information Provided" Count="13" />
       <Dispositions Result="Lost Opportunity" Count="1" />
       <Dispositions Result="No Agent Available" Count="1" />
       <Dispositions Result="Ring No Answer" Count="6" />
       <Dispositions Result="SIT Tone" Count="3" />
       <Dispositions Result="Tour / Sale / Booking" Count="7" />
     </Products>
   </Countries>
   <Countries Country="Italy">
     <Products Product="Office">
       <Dispositions Result="Did Not Leave Voicemail" Count="1" />
       <Dispositions Result="Information Provided" Count="6" />
       <Dispositions Result="Non-Sales Related" Count="1" />
       <Dispositions Result="Other" Count="6" />
       <Dispositions Result="Ring No Answer" Count="7" />
       <Dispositions Result="SIT Tone" Count="5" />
       <Dispositions Result="Updated Existing Enquiry" Count="13" />
     </Products>
   </Countries>
   <Countries Country="Netherlands">
     <Products Product="Office">
       <Dispositions Result="Agent Voicemail / Message" Count="1" />
       <Dispositions Result="Information Provided" Count="17" />
       <Dispositions Result="Ring No Answer" Count="3" />
       <Dispositions Result="Tour / Sale / Booking" Count="2" />
     </Products>
  </Countries>
 </DialStats>

可能有许多不同的国家。

谢谢 输出量

(编辑)

我建议您尝试其他方法。 以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="categories">
    <category row="1">Agent Voicemail / Message</category>
    <category row="1">Did Not Leave Voicemail</category>
    <category row="1">Information Provided</category>
    <category row="1">Lost Opportunity</category>
    <category row="1">No Agent Available</category>
    <category row="1">Ring No Answer</category>
    <category row="2">SIT Tone</category>
    <category row="2">Tour / Sale / Booking</category>
    <category row="2">Non-Sales Related</category>
    <category row="2">Other</category>
    <category row="2">Updated Existing Enquiry</category>
</xsl:variable>

<xsl:variable name="col-set" select="exsl:node-set($categories)/category" />

<xsl:template match="/">
    <xsl:for-each select="DialStats/Countries">
        <xsl:variable name="country" select="." />
        <table border="1" style="width:30%; display:inline-block">
            <tr>
                <th colspan="{count($col-set[@row='1'])}">
                    <xsl:value-of select="$country/@Country"/>
                </th>
            </tr>
            <tr>
                <xsl:for-each select="$col-set[@row='1']">
                    <th><xsl:value-of select="."/></th>
                </xsl:for-each>
            </tr>
            <tr>
                <xsl:for-each select="$col-set[@row='1']">
                    <td>
                        <xsl:value-of select="$country/Products/Dispositions[@Result=current()]/@Count"/>
                    </td>
                </xsl:for-each>
            </tr>
            <tr>
                <xsl:for-each select="$col-set[@row='2']">
                    <th><xsl:value-of select="."/></th>
                </xsl:for-each>
            </tr>
            <tr>
                <xsl:for-each select="$col-set[@row='2']">
                    <td>
                        <xsl:value-of select="$country/Products/Dispositions[@Result=current()]/@Count"/>
                    </td>
                </xsl:for-each>
            </tr>
        </table>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

当应用于您的输入示例时,将产生:

<?xml version="1.0" encoding="UTF-8"?>
<table border="1" style="width:30%; display:inline-block">
   <tr>
      <th colspan="6">France</th>
   </tr>
   <tr>
      <th>Agent Voicemail / Message</th>
      <th>Did Not Leave Voicemail</th>
      <th>Information Provided</th>
      <th>Lost Opportunity</th>
      <th>No Agent Available</th>
      <th>Ring No Answer</th>
   </tr>
   <tr>
      <td>11</td>
      <td>37</td>
      <td>13</td>
      <td>1</td>
      <td>1</td>
      <td>6</td>
   </tr>
   <tr>
      <th>SIT Tone</th>
      <th>Tour / Sale / Booking</th>
      <th>Non-Sales Related</th>
      <th>Other</th>
      <th>Updated Existing Enquiry</th>
   </tr>
   <tr>
      <td>3</td>
      <td>7</td>
      <td/>
      <td/>
      <td/>
   </tr>
</table>
<table border="1" style="width:30%; display:inline-block">
   <tr>
      <th colspan="6">Italy</th>
   </tr>
   <tr>
      <th>Agent Voicemail / Message</th>
      <th>Did Not Leave Voicemail</th>
      <th>Information Provided</th>
      <th>Lost Opportunity</th>
      <th>No Agent Available</th>
      <th>Ring No Answer</th>
   </tr>
   <tr>
      <td/>
      <td>1</td>
      <td>6</td>
      <td/>
      <td/>
      <td>7</td>
   </tr>
   <tr>
      <th>SIT Tone</th>
      <th>Tour / Sale / Booking</th>
      <th>Non-Sales Related</th>
      <th>Other</th>
      <th>Updated Existing Enquiry</th>
   </tr>
   <tr>
      <td>5</td>
      <td/>
      <td>1</td>
      <td>6</td>
      <td>13</td>
   </tr>
</table>
<table border="1" style="width:30%; display:inline-block">
   <tr>
      <th colspan="6">Netherlands</th>
   </tr>
   <tr>
      <th>Agent Voicemail / Message</th>
      <th>Did Not Leave Voicemail</th>
      <th>Information Provided</th>
      <th>Lost Opportunity</th>
      <th>No Agent Available</th>
      <th>Ring No Answer</th>
   </tr>
   <tr>
      <td>1</td>
      <td/>
      <td>17</td>
      <td/>
      <td/>
      <td>3</td>
   </tr>
   <tr>
      <th>SIT Tone</th>
      <th>Tour / Sale / Booking</th>
      <th>Non-Sales Related</th>
      <th>Other</th>
      <th>Updated Existing Enquiry</th>
   </tr>
   <tr>
      <td/>
      <td>2</td>
      <td/>
      <td/>
      <td/>
   </tr>
</table>

呈现为:

在此处输入图片说明

该表的结构是恒定的,并且不依赖于传入的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM