We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,470 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

XSLT - for-each nestled problem

Hi,
I have a problem with a nestled "for-each". I want information from every orditem. Every orderitem have one "orderitem id" and could have several "job id". The sample belove do not work,any suggestions?

XML

<order orderid="20">
  <workflows>
    <workflow id="97"
      <items>
        <orderitem id="1">
          <jobs>
            <job id="150"/>
            <job id="151"/>
          </jobs>
        </orderitem>
      </items>
    </workflow>
    <workflow id="97">
      <items>
        <orderitem id="2">
          <jobs>
            <job id="152"/>
          </jobs>
        </orderitem>
      </items>
    </workflow>
  </workflows>
</order>

XSLT

    <xsl:for-each select="//items/orderitem[@id != '0']">
          <xsl:value-of select="@item-number"/>
      <xsl:for-each select="/order/jobs/job">
            <xsl:value-of select="@id"/>
          </xsl:for-each>
    </xsl:for-each>

I want the output to be:
1 150 151
2 152

Kind regards / trady

3
Contributors
2
Replies
2 Weeks
Discussion Span
1 Year Ago
Last Updated
3
Views
trady
Newbie Poster
1 post since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Hi Trady,

I got the result layout you wanted using the following XSLT stylesheet:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:cs="urn:cs"
  exclude-result-prefixes="cs msxsl" >

  <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

  <xsl:template match="order">
    <xsl:apply-templates select="workflows"/>
  </xsl:template>

  <xsl:template match="workflows">
    <xsl:for-each select="workflow/items/orderitem">
      <xsl:value-of select="@id"/>
      <xsl:text>&#32;</xsl:text>
      <xsl:for-each select="jobs/job">
        <xsl:value-of select="@id"/>
        <xsl:text>&#32;</xsl:text>
      </xsl:for-each>
      <xsl:text>&#0010;</xsl:text>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

Hope this helps :)

MikeyIsMe
Practically a Posting Shark
802 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10

do not think as programmers
the loops are therefore not necessary
think in terms of how it was developed template xslt

Use only the nodes where the information is contained

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="no"/>
<xsl:template match="/">
<xsl:apply-templates select="child::*//jobs"/>
</xsl:template>
<xsl:template match="jobs">
<xsl:value-of select="position()"/>
<xsl:apply-templates select="job"/>
<xsl:text>&#10;</xsl:text>
</xsl:template>
<xsl:template match="job">
<xsl:value-of select="concat(' ',@id)"/>
</xsl:template>
</xsl:stylesheet>

xml_looser
Junior Poster
180 posts since Apr 2009
Reputation Points: 16
Solved Threads: 21
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0612 seconds using 2.7MB