Solved

Help performing a relative XSLT transformation

Posted on 2006-12-01
4
452 Views
Last Modified: 2012-05-05
Help performing a relative XSLT transformation

I have this page:
http://pdn.sieris.com/sieris/core8/example-page.php

in the XML I have:
<block>
      <text>
            random number -&gt; 760<br/>And 8 line breaks.<br/><br/><br/><br/><br/><br/><br/><br/>textbox ->
      </text>
      <textbox>
            <name>address</name>
      </textbox>
</block>



Then I have this as my XSLT:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" omit-xml-declaration="yes"/>
      <xsl:include href="widgets/block.xslt"/>
      <xsl:include href="widgets/textbox.xslt"/>
      <xsl:include href="widgets/text.xslt"/>
      <xsl:template name="widgets">
            <xsl:param name="thisNode"/>
            <xsl:for-each select="$thisNode/child::block">
                  <xsl:call-template name="block">
                        <xsl:with-param name="thisNode" select="."/>
                  </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="$thisNode/child::textbox">
                  <xsl:call-template name="textbox">
                        <xsl:with-param name="thisNode" select="."/>
                  </xsl:call-template>
            </xsl:for-each>
            <xsl:for-each select="$thisNode/child::text">
                  <xsl:call-template name="text">
                        <xsl:with-param name="thisNode" select="."/>
                  </xsl:call-template>
            </xsl:for-each>
      </xsl:template>
</xsl:stylesheet>


Problem is... this is my output:
<div><input name="address" type="text">random number -&gt; 98<br>And 9 line breaks. <br><br><br><br><br><br><br><br><br>textbox -&gt;</div>


Please help... I want everything to transform at it's current location. No matter how "deep" of a node it is. I want to be able to put these elements inside of each other with no limit.
Notice that the textbox is showing up before everything else. It is not showing up after the "text". The only part that is working correctly is that is is showing up within the "block", which is the div tag.
0
Comment
Question by:phobis
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 18054241
Hi phobis,

having three for-each statements, you pull the nodes out of order
if you want to keep them in sync you will need something like this

          <xsl:for-each select="$thisNode/block | $thisNode/textbox | $thisNode/text">
                <xsl:choose>
                    <xsl:when test="name() = 'block'">
                          <xsl:call-template name="block">
                                <xsl:with-param name="thisNode" select="."/>
                           </xsl:call-template>
                    </xsl:when>
                    <xsl:when test="name() = 'textbox '">
                          <xsl:call-template name="'textbox">
                                <xsl:with-param name="thisNode" select="."/>
                           </xsl:call-template>
                    </xsl:when>
                    <xsl:when test="name() = block">
                          <xsl:call-template name="block">
                                <xsl:with-param name="thisNode" select="."/>
                           </xsl:call-template>
                    </xsl:when>
                </xsl:choose>
          </xsl:for-each>

This way, you don't change the order

Cheers!
0
 

Author Comment

by:phobis
ID: 18054341
Hi Gerton,

I tried your example:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" omit-xml-declaration="yes"/>
      <xsl:include href="widgets/block.xslt"/>
      <xsl:include href="widgets/textbox.xslt"/>
      <xsl:include href="widgets/text.xslt"/>
      <xsl:template name="widgets">
            <xsl:param name="thisNode"/>
            <xsl:for-each select="$thisNode/block | $thisNode/textbox | $thisNode/text">
                <xsl:choose>
                    <xsl:when test="name()='block'">
                          <xsl:call-template name="block">
                                <xsl:with-param name="thisNode" select="."/>
                           </xsl:call-template>
                    </xsl:when>
                    <xsl:when test="name()='textbox'">
                          <xsl:call-template name="textbox">
                                <xsl:with-param name="thisNode" select="."/>
                           </xsl:call-template>
                    </xsl:when>
                    <xsl:when test="name()='block'">
                          <xsl:call-template name="block">
                                <xsl:with-param name="thisNode" select="."/>
                           </xsl:call-template>
                    </xsl:when>
                </xsl:choose>
          </xsl:for-each>
      </xsl:template>
</xsl:stylesheet>


Now my output looks like this:
<div><input name="address" type="text"></div>

It should look like this:
<div>random number -&gt; 98<br>And 9 line breaks. <br><br><br><br><br><br><br><br><br>textbox -&gt;<input name="address" type="text"></div>

it is currently at:
http://pdn.sieris.com/sieris/core8/example-page.php
(which seems to only work in Firefox right now, due to some javascript on the page that needs to be fixed)
0
 

Author Comment

by:phobis
ID: 18054377
LOL, sorry.

This was my fault... I did not see that
<xsl:when test="name()='block'">
                 <xsl:call-template name="block">
                         <xsl:with-param name="thisNode" select="."/>
                  </xsl:call-template>
</xsl:when>

Was listed twice  :)

I changed the second "block" block to be "text"

thank you!!! it all works now :)
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18054527
you are welcome,
sorry about my typo

cheers

Geert
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now