XSLT for each loop on simple XML

37,752

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

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

<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>
Share:
37,752
None
Author by

None

Updated on July 09, 2022

Comments

  • None
    None almost 2 years

    I have an XML like this

          <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

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

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

    Complete 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

        <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>
    
  • None
    None over 11 years
    :as far as i had understood code execution does not happen inside this for each loop
  • None
    None over 11 years
    i mean select="filters/ISP_WebItem " condition never satisfies in my code. i have modified my question with the details.if you still dont understand please tell me
  • rene
    rene over 11 years
    Is filters the root element? I added a / in my answer
  • None
    None over 11 years
    this one also is not working. <xsl:copy-of select="filters"/> is correctly giving me the XML i wrote in the question
  • rene
    rene over 11 years
    with your xml and mine xslt I have output, so it must be an environment issue. Notice i removed the extra space at the end of the select.
  • None
    None over 11 years
    rene : I am using xml version="1.0" .that is the only thing i can tell you.