XML 文件中不同节点的 XSL [英] XSL for each for different nodes in XML file

查看:32
本文介绍了XML 文件中不同节点的 XSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 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>

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

I want to select the SiteName for each CourseEntity. For example for the CourseID = 50 the SiteName should be Ramada Watford.

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

So far I have this XSL but it doesn't work.

<?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 的相关站点名称.

The first for-each loop runs through CourseEntities and the inner loop is trying to find the relevant site name for each course id.

有什么想法吗?

输出

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

推荐答案

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

For-each loops are normally best avoided in XSLT. Try this templated approach.

可在 此 XMLPlayground

<!-- 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>

这篇关于XML 文件中不同节点的 XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆