Help - Search - Members - Calendar
Full Version: XSL Transformation problem
1Emulation.Com > Free For All > Computer Speak
Agozer
So yeah, I having trouble with an XML --> XHTML transformation using XSL. More specifically I want to count the number of nodes with specific data and then output the result.

My XML structure is something like this:

CODE
<people>

  <person>
    <name>Mary</name>
    <sex>Yes please</sex>
    <note>bring condoms, bring wine, wears expensive lingerie</note>
  </person>

  <person>
    <name>Felli</name>
    <sex>Yes please</sex>
    <note>loli with a twist, mean eye</note>
  </person>

  <person>
    <name>Helga</name>
    <sex>Hell no</sex>
    <note>Huge girth, needs eyebleach</note>
  </person>

  <person>
    <name>Jenna</name>
    <sex>Yes please</sex>
    <note>bring wine, pornstar, blonde</note>
  </person>

</people>

...

etc. etc.


What I'm trying to do is to count (using XSLT count() function or any other suitable XSLT method) the number of persons with both the sex element containing "yes please" as well as the note element containing the words "bring wine". I have traversed the Intarwebs for months, steering asway from porn, searching high and low, and have found lots of would-be examples, just not the one I'm looking for. All my past efforts have produced incorrect results or a result of 0, so there's something I'm doing wrong - I just can't figure out what.
LoRd_SnOw
My XML Knowledge is pretty poor, but maybe this can help, which is an example i made about a year ago.


save your .xml file to data.xml

CODE
<site>
<post>
     <text>.</text>
     <categories>
       <category>Food</category>
       <category>Toaster</category>
       <category>Piano</category>
     </categories>          
</post>

<post>
     <text>.</text>
      <categories>
       <category>Toaster</category>
       <category>Piano</category>
      </categories>    
  </post>

<post>
     <text>.</text>
     <categories>
      <category>Piano</category>
     </categories>
</post>
<post>
     <text>.</text>
     <categories>
<category>Food</category>
      <category>Piano</category>
     </categories>
</post>
</site>

write your xslt, then name it as transform.xslt

CODE
<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="/">

  <xsl:for-each select="//category">
   <xsl:variable name="value" select="."/>
   <xsl:if test="count(preceding::category[.=$value]) = 0">
    <xsl:value-of select="."/>
    <xsl:text> (</xsl:text>
    <xsl:value-of select="count(//category[.=$value])"/>    
    <xsl:text>)</xsl:text><br/>
   </xsl:if>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>



when you open in IE the following results will be categories listed, for example: Food(2) Toaster(3) Piano(4)
Agozer
Not what I was looking for, but a good example of the use of for-each that I can use in my future projects.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.