简单 XML 上每个循环的 XSLT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14053971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:49:50  来源:igfitidea点击:

XSLT for each loop on simple XML

xmlxsltxslt-1.0

提问by None

I have an XML like this

我有一个这样的 XML

      <filters extra="filters">
<ISP_WebItem FILTER="Farve" FILTERNAME="Sort" UNITCODE=""/>
<ISP_WebItem FILTER="L?ngde" FILTERNAME="51" UNITCODE="cm"/>
<ISP_WebItem FILTER="H?jde" FILTERNAME="3.2" UNITCODE="cm"/>
<ISP_WebItem FILTER="Dybde" FILTERNAME="9" UNITCODE="cm"/>
<ISP_WebItem FILTER="Stavl?ngde" FILTERNAME="11" UNITCODE="cm"/>
      </filters>

I want to loop through each ISP_WebItem and display FILTER,FILTERNAME and UNITCODE. i have tried some thing like this

我想遍历每个 ISP_WebItem 并显示 FILTER、FILTERNAME 和 UNITCODE。我试过这样的事情

<xsl:for-each select="filters/ISP_WebItem ">              
          <xsl:value-of  select="FILTER" />
          <xsl:value-of  select="FILTERNAME" />
          <xsl:value-of  select="UNITCODE" />
 </xsl:for-each>

but of no use. and when i put a break point and checked ,I found that code execution does not happen inside for each loop( break point inside for each loop never hits).

但没有用。当我放置一个断点并检查时,我发现每个循环内部不会发生代码执行(每个循环内部的断点永远不会命中)。

I have limited knowledge on XSLT and i know this may be a simple question .but i really need to overcome this.can any one guide me on this.

我对 XSLT 的了解有限,我知道这可能是一个简单的问题。但我真的需要克服这个问题。任何人都可以指导我。

note: as some people requested for complete XSLT and XML i am publishing it here

注意:由于有些人要求提供完整的 XSLT 和 XML,因此我将其发布在这里

Complete XSLT

完整的 XSLT

<xsl:template match="/">      
<xsl:variable name="p">
  <xsl:choose>
    <xsl:when test="library:Request('pid') != ''">
      <xsl:copy-of select="shop:GetProductFromId(library:Request('pid'))" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:copy-of  select="shop:GetProductFromId(shop:UrlInformation()//productid)" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>
<xsl:copy-of select="msxsl:node-set($p)/product/filters"/>
<xsl:for-each select="msxsl:node-set($p)/product/filters/ISP_WebItem">
    <xsl:value-of  select="@FILTER" />
    <xsl:value-of  select="@FILTERNAME" />
    <xsl:value-of  select="@UNITCODE" />
  </xsl:for-each>
</xsl:template>

Complete XML

完整的 XML

    <product>
  <estocklevel>0</estocklevel>
  <url>/product/relief-smal-brevordner-bordeaux-2</url>
  <texts>
    <text language="standard">
      <name>Relief Smal Brevordner, Bordeaux (2)</name>
      <longdescription></longdescription>
      <shortdescription>(10)</shortdescription>
      <htmltitle></htmltitle>
      <metadescription></metadescription>
      <metakeywords></metakeywords>
    </text>
  </texts>
  <name>Relief Smal Brevordner, Bordeaux (2)</name>
  <longdescription></longdescription>
  <shortdescription>(10)</shortdescription>
  <htmltitle></htmltitle>
  <metadescription></metadescription>
  <metakeywords></metakeywords>
  <alternativeitemid></alternativeitemid>
  <alternativeitemrule>0</alternativeitemrule>
  <duties />
  <oncampaign extra="oncampaign">0</oncampaign>
  <minweb extra="minweb">0.000000000000</minweb>
  <stockitem extra="stockitem">0.000000000000</stockitem>
  <isp_model extra="isp_model">Smal</isp_model>
  <produkttype extra="produkttype"></produkttype>
  <filters extra="filters">
    <ISP_WebItem FILTER="Farve" FILTERNAME="Bordeaux" UNITCODE=""/>
    <ISP_WebItem FILTER="Rygbredde" FILTERNAME="5" UNITCODE="cm"/>
    <ISP_WebItem FILTER="Papirst?rrelse" FILTERNAME="A4" UNITCODE=""/>
    <ISP_WebItem FILTER="Max indhold" FILTERNAME="350 A4 ark" UNITCODE=""/>
    <ISP_WebItem FILTER="Rygetiket" FILTERNAME="Med udskiftelig rygetiket" UNITCODE=""/>
    <ISP_WebItem FILTER="Materiale 1" FILTERNAME="PP" UNITCODE=""/>
    <ISP_WebItem FILTER="Materiale" FILTERNAME="Pap" UNITCODE=""/>
    <ISP_WebItem FILTER="Ringtype" FILTERNAME="D" UNITCODE=""/>
    <ISP_WebItem FILTER="Antal ringe" FILTERNAME="2" UNITCODE=""/>
  </filters>
</product>

回答by rene

select the attributes by using the @ to distinghish attributes from elements. Tested on xslfiddle.net

通过使用@ 来区分属性和元素来选择属性。在xsfiddle.net 上测试

xsl

xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="iso-8859-1" indent="no"/>

<xsl:template match="/">
    <xsl:for-each select="filters/ISP_WebItem">
      <xsl:value-of  select="@FILTER" />
      <xsl:value-of  select="@FILTERNAME" />
      <xsl:value-of  select="@UNITCODE" />
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>  

xml

xml

<filters extra="filters">
    <ISP_WebItem FILTER="Farve" FILTERNAME="Sort" UNITCODE=""/>
    <ISP_WebItem FILTER="L?ngde" FILTERNAME="51" UNITCODE="cm"/>
    <ISP_WebItem FILTER="H?jde" FILTERNAME="3.2" UNITCODE="cm"/>
    <ISP_WebItem FILTER="Dybde" FILTERNAME="9" UNITCODE="cm"/>
    <ISP_WebItem FILTER="Stavl?ngde" FILTERNAME="11" UNITCODE="cm"/>
</filters>

result

结果

<html><head></head><body>FarveSortL?ngde51cmH?jde3.2cmDybde9cmStavl?ngde11cm</body></html>