tags:

views:

136

answers:

2

Hi all:

I'm totally stuck on what is going on when I try to run the following xml against an xsl in an attempt to not get a blank page in the browser:

xml:

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
<Structure>
    <Processes>
        <Process>
            .
            .
            .
        </Process>
    </Processes>
</Structure>

xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <!--trying to convert the tag to another name -->
    <xsl:template match="/">
        <xsl:element name="NewStructure">
            <xsl:apply-templates select="Processes" />
        </xsl:element>
    </xsl:templates>
    <xsl:template name="convert_processes_tag" match="Processes">
        <xsl:for-each select="Processes">
            <xsl:element name="NewProcess" />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

My goal is to replace the Processes tag with something else, and subsequently all the other tags inside the xml too. My first attempt was to convert the outter most tags first. I have tried cutting down both the xml and the xsl to its skeleton, but all I got was a blank page. I tried googling, but it only further confuses me. I know xsl:template is like a method in OOP, and I wanted to separate it because there will be a lot of looping involved in converting the tags to different names.

Been stuck for hours... any help would be appreciated.

EDIT: I was testing this against IE. Someone has pointed out that XML does not show up when testing in IE. Is that the case?

TIA.

+4  A: 

XSLT does all the tree traversing for you. You don't need to use any for-each loops or named templates. You don't even need to care which elements are converted before which other elements.

All you need to do is specify what you want to happen to any XML node you encounter.

<Structure>        <!-- write a template that matches "Structure" elements -->
    <Processes>    <!-- write a template that matches "Processes" elements -->
        <Process>  <!-- write a template that matches "Process" elements -->
        </Process>
    </Processes>
</Structure>

so, this should already get you pretty far:

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

  <xsl:template match="Structure">
    <NewStructure>
      <xsl:apply-templates />  <!-- apply-templates is the recursive step -->
    </NewStructure>
  </xsl:template>

  <xsl:template match="Processes">
    <NewProcesses>  <!-- note that you don't even need <xsl:element> -->
      <xsl:apply-templates />
    </NewProcesses>
  </xsl:template>

  <xsl:template match="Process">
    <NewProcesses>
      <xsl:apply-templates />
    </NewProcesses>
  </xsl:template>

  <!-- this empty template prevents blank text nodes from leaking through -->
  <xsl:template match="text()[normalize-space() = '']" />

</xsl:stylesheet>
Tomalak
@Tomalak: I'm still getting a blank page... totally white IE browser. Is there something wrong with the headers in the xml or xsl document? I have the exact headers in my documents.
BeraCim
@Tomalak: your solution helped me in clarifying how to write xsl the proper way. I was applying OOP thinking to xsl, and that thinking was supported by googling. Thanks for pointing out the easier way of writing xsl. +1.
BeraCim
@BeraCim: Glad to help.
Tomalak
+1  A: 

A browser is not the right tool if you want to debug XSLT that transforms XML to XML, unless it is a browser like Firefox or Opera and your target XML format is a format like SVG or MathML or XHTML the browser knows to render. IE is useful if you transform XML to HTML (xsl:output method="html") or maybe plain text (xsl:output method="text") but when you use it to transform XML to XML (xsl:output method="xml") then don't expect you to show anything but the text nodes in the result document. And in your case you do not even have any text nodes that contain non white space characters so there is nothing to display.

Martin Honnen
@Martin Honnen: i don't know who downvoted you, but what you said about how the broswer won't display xml was why I got a blank page. I used Visual studio's xslt tool and I can now see the outputs. Not sure about the text node thingy though, but at least I can see the xslt outputs now. Thanks! +1
BeraCim
@BeraCim: Try MarrowSoft Xselerator, it is a decent free XSLT debugger that uses MSXML (just like the Internet Explorer, should that be your target platform).
Tomalak