Solved

XSLT Sarissa XML

Posted on 2011-09-13
6
431 Views
Last Modified: 2012-05-12
Hi,

I have some XML. I am trying to create a structure that works in much the same way as Windows Explorer. My XSL seems to break down here, on my for loop, which I basically cut and paste from another source.

<xsl:for-each select="key('obj', $detail-id)/*[not(self::name)]">        

I was hoping someone would know how I can get it too render properly. So that I can click on an item and it goes into the next level displaying all of these items. I am using sarissa, I can gladly provide the .js though I think it is problem with this loop structure where the problem exists.

NOTE- The below code is only done for type 'folder', it is intended for type folder and file, both of which can be placed at any level, and can have each other inside each other.

Thanks in advance.
Here is xml example:

<?xml version="1.0" encoding="utf-8"?>
<testData>
  <items id="0">
    <item id="1" name="Item1" type="file">
      <item id="4" name="Item4" type="folder">
        <item id="5" name="Item5" type="file"/>
        <item id="6" name="Item6" type="folder" />
      </item>
      <item id="2" name="Item2" type="folder" />
        <item id="3" name="Item3" type="folder"  />
      </item>
   </items>
</testData>

Here is xsl structure:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="obj" match="*" use="@id"/>
  <xsl:param name="detail-id">sms</xsl:param>
  <xsl:template match="testData">
  <xsl:apply-templates />
  </xsl:template>
<xsl:template match="item/*">
      <xsl:for-each select="key('obj', $detail-id)/*[not(self::name)]">  
        <xsl:choose>
          <xsl:when test="@type = 'folder'">
            <span id="{@id}" >
              <xsl:attribute name="onclick">
                <xsl:text>DisplayResult('</xsl:text>
                <xsl:value-of select="@id"/>
                <xsl:text>','</xsl:text>
                <xsl:value-of select="name()"/>
                <xsl:text>');</xsl:text>
              </xsl:attribute>
              <xsl:value-of select="@name"></xsl:value-of>
            </span>
          </xsl:when>
        </xsl:choose>        
      </xsl:for-each>
  </xsl:template>
  </xsl:stylesheet>

Open in new window

0
Comment
Question by:metta0_3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 36530047
If this is XSLT for Sarissa, it is bound to break,
Sarissa requires wellformed XML as a transform result... meaning one single root

further more, it is not very clear what you need.
Do you want to expand one further level given the id?
that the following is about what you need

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:key name="obj" match="*" use="@id"/>
    <xsl:param name="detail-id">1</xsl:param>
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="testData">
        <!-- relies on teh key only returning one object -->
        <xsl:apply-templates select="key('obj', $detail-id)"/>
    </xsl:template>
    <xsl:template match="item"><!-- actually only the keyed one -->
        <div><!-- creating a container for Sarissa -->
            <xsl:copy-of select="@id | @name"/> <!-- at least copying the id for later overwrite -->
            <xsl:for-each select="*">
                <xsl:if test="@type = 'folder'">
                    <span id="{@id}" >
                        <xsl:attribute name="onclick">
                            <xsl:text>DisplayResult('</xsl:text>
                            <xsl:value-of select="@id"/>
                            <xsl:text>','</xsl:text>
                            <xsl:value-of select="name()"/>
                            <xsl:text>');</xsl:text>
                        </xsl:attribute>
                        <xsl:value-of select="@name"></xsl:value-of>
                    </span>
                </xsl:if>
            </xsl:for-each>        
        </div>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36530055
now you need a smart override, by letting the XSL result override the outer html of the identified div
that is how you can close the stuff back if you want later
0
 

Author Comment

by:metta0_3
ID: 36530220
What you have provided is pretty close. Thanks. detail-id set to 1 is causing an issue for me. Its for a search filter, so a user might decide to start the search from another item within the tree. The xml is actually the result based on what is sent to the server by the user.

<xsl:param name="detail-id">1</xsl:param>

Open in new window


For example the result might be further in the tree like this

<?xml version="1.0" encoding="utf-8"?>
<testData>
  <items id="0">
      <item id="4" name="Item4" type="folder">
        <item id="5" name="Item5" type="file"/>
        <item id="6" name="Item6" type="folder" />
      </item>
      <item id="2" name="Item2" type="folder" />
        <item id="3" name="Item3" type="folder"  />
      </item>
</testData>

Open in new window


Therefore item 4 and 2 are the first to be displayed. Also it is unlimited the levels that I wish to be able to click through.

0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

Author Comment

by:metta0_3
ID: 36530268
Ok this seems to work as I always have the root element id=0. I think I am there.
<xsl:param name="detail-id">1</xsl:param>

Open in new window


Athough can I do this, just to save code.

<xsl:if test="@type = 'folder'" | "@type='file'>

Open in new window

0
 

Author Comment

by:metta0_3
ID: 36530296
The above comment was meant to say this.

<xsl:param name="detail-id">1</xsl:param>

Open in new window


Now I just want to shorten the below as I am producing unnecessary code.

<xsl:for-each select="*">
          <xsl:if test="@type = 'folder'">
            <span id="{@id}" >
              <xsl:attribute name="onclick">
                <xsl:text>DisplayOverview('</xsl:text>
                <xsl:value-of select="@id"/>
                <xsl:text>','</xsl:text>
                <xsl:value-of select="name()"/>
                <xsl:text>');</xsl:text>
              </xsl:attribute>
              <xsl:value-of select="@name"></xsl:value-of>
            </span>
          </xsl:if>
          <xsl:if test="@type = 'file'">
            <span id="{@id}" >
              <xsl:attribute name="onclick">
                <xsl:text>DisplayOverview('</xsl:text>
                <xsl:value-of select="@id"/>
                <xsl:text>','</xsl:text>
                <xsl:value-of select="name()"/>
                <xsl:text>');</xsl:text>
              </xsl:attribute>
              <xsl:value-of select="@name"></xsl:value-of>
            </span>
          </xsl:if>
        </xsl:for-each>

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 36532186
Hi,

was gone for a while, seems you fixed it by now

<xsl:if test="@type = 'folder'" | "@type='file'>

should be

<xsl:if test="@type = 'folder'" or "@type='file'>
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question