每个XSL用于XML文件中的不同节点

时间:2012-08-06 11:02:12

标签: xml xslt transformation

这是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet  type="text/xsl" href="coursestyle.xsl"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <ns0:FindCoursesForOffenderResponse xmlns:ns0="http://H.FindCoursesForOffenderResponse">
         <ns0:SiteList>
            <ns0:SiteEntity>
               <ns0:SiteId>10</ns0:SiteId>
               <ns0:SiteName>Ramada Watford</ns0:SiteName>
            </ns0:SiteEntity>
            <ns0:SiteEntity>
               <ns0:SiteId>20</ns0:SiteId>
               <ns0:SiteName>Ramada Jarvis (Comet) Hotel</ns0:SiteName>
            </ns0:SiteEntity>
         </ns0:SiteList>
         <ns0:CourseList>
            <ns0:CourseEntity>
               <ns0:CourseId>50</ns0:CourseId>
               <ns0:SiteId>10</ns0:SiteId>
            </ns0:CourseEntity>
            <ns0:CourseEntity>
               <ns0:CourseId>10</ns0:CourseId>
               <ns0:SiteId>10</ns0:SiteId>
            </ns0:CourseEntity>
            <ns0:CourseEntity>
               <ns0:CourseId>20</ns0:CourseId>
               <ns0:SiteId>20</ns0:SiteId>
            </ns0:CourseEntity>
         </ns0:CourseList>
      </ns0:FindCoursesForOffenderResponse>
   </s:Body>
</s:Envelope>

我想为每个SiteName选择CourseEntity。例如,对于CourseID = 50SiteName应为Ramada Watford

到目前为止,我有这个XSL,但它不起作用。

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://H.FindCoursesForOffenderResponse" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<xsl:output method="html"/>
<xsl:param  name="lnum">123</xsl:param>

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

    <xsl:for-each select="s:Envelope/s:Body/ns0:FindCoursesForOffenderResponse/ns0:CourseList/ns0:CourseEntity">
        <xsl:variable  name="currEntity"><xsl:value-of select="ns0:SiteId"/></xsl:variable>
            <xsl:value-of select="$currEntity"/><br/>
            <xsl:for-each select="s:Envelope/s:Body/ns0:FindCoursesForOffenderResponse/ns0:SiteList/ns0:SiteEntity[ns0:SiteId=$currEntity]">            
          <li>
            <xsl:value-of select="ns0:SiteName"/>

          </li>
              </xsl:for-each>
          </xsl:for-each>

    </ul>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

第一个for-each循环遍历CourseEntities,内循环正在尝试查找每个课程ID的相关站点名称。

有什么想法吗?

输出

<couseID> - <sitename>
50 - Ramada Watford
20 -   Ramada Jarvis (Comet) Hotel

1 个答案:

答案 0 :(得分:3)

在XSLT中通常最好避免For-each循环。试试这个模板化的方法。

this XMLPlayground

上的Runnable演示
<!-- kick things off -->
<xsl:template match="s:Envelope/s:Body/ns0:FindCoursesForOffenderResponse">
    <ul>
        <xsl:apply-templates select='ns0:CourseList/ns0:CourseEntity' />
    </ul>
</xsl:template>

<!-- site entities... -->
<xsl:template match='ns0:CourseEntity'>
    <li>
        <xsl:value-of select='ns0:CourseId' />
        -
        <!-- ...find corresponding site name -->
        <xsl:value-of select='../../ns0:SiteList/ns0:SiteEntity[ns0:SiteId = current()/ns0:SiteId]/ns0:SiteName' />
    </li>
</xsl:template>