【问题标题】:XSL for each for different nodes in XML fileXML文件中不同节点的XSL
【发布时间】:2012-08-03 08:23:45
【问题描述】:

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

到目前为止,我有这个 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

【问题讨论】:

  • 你想要什么输出?请编辑要显示的问题。

标签: xml xslt transformation


【解决方案1】:

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

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

【讨论】:

  • 非常感谢 Utkanos 的回答。有效。我不知道它是如何工作的。 :(
  • 很多人在开始 XSL 时就陷入了 for-each,因为这是他们从其他语言中得到的。 XSLT 的工作方式不同,需要不同的思维方式。基本上我在那里所做的是将模板(XSLT 的关键概念)应用于某些节点——也就是说,通过特定的转换运行它们。然后我查找树并返回内部以找到相应的站点名称。 XSLT 可能需要一段时间才能让你明白,但最终它很容易 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多