1

I know that's easy, but I tried and XSLT is really non-understandable for me... That is why I would like to have an example over my code so I could examine it on real-case example and probably understand it.

So I have app that has GUI tree view and generates me XML of this sort:

<TreeView>
  <Parent text="Installation">
    <child text="Startup" type="video" file="startup.wmv" icon="c://user/desktop/blahblah.jpg"/>
    <child text="Getting there" type="video" file="gettingthere.wmv" icontPath="something"/>
    <child text="Steps" type="document" file="steps.docx" icon="asd"/>
    <child text="Pictures" type="image" file="pics.jpg" icon="dasdasdas"/>
  </Parent>
  <Parent text="Usage">
    <child text="Tilbud pane" type="video" file="tilbud.mwv" icon="asdasd"/>
    <child text="Report pane" type="document" file="report.docx" icon="gfdhd"/>
  </Parent>
</TreeView>

Then I need to transform this XML into HTML so I can update it in my website.

So I don't need the <html> and <body> tags. I need just the ordering of this XML into a list, whereas there should be some space before the child elements.

A desired output, viewed by the user in the browser will be like that:

Installation
  Startup
  Getting there
  Steps
  Pictures
Usage
  Tilbud pane
  Report pane

But because I will not have the same values in the attributes - I have to order upon the elements.

This is what I have:

<?xml version = "1.0" encoding = "utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <ul>
      <xsl:for-each select="Parent">
        <li>
        <xsl:value-of select="@text"/>
          <ul>
            <xsl:for-each select="Parent/child"/>
            <li>
            <xsl:value-of select="@text"/>
            </li>
            </xsl:for-each>
          </ul>
        </li>
      </xsl:for-each>
    </ul>
  </xsl:template>
</xsl:stylesheet>

and the desired html I have to get is smth like this:

<ul>
    <li>Parent
         <ul>
               <li>Child</li>
               <li>Child</li>
 .....
         </ul>
    </li>
   <li>Parent
         <ul>
               <li>Child</li>
               <li>Child</li>
 .....
         </ul>
   </li>
.....
</ul>

but apparently it doesn't want to give me this... It gives me just <ul/> after I run the transformation...

1

2 Answers 2

1

Your XSLT does not work because it searches Parent directly at / but there is TreeView in between.

<xsl:template match="/TreeView">
   ...
Sign up to request clarification or add additional context in comments.

2 Comments

You were right about that, but I still don't get the children in the output. I get just the Parents.
<xsl:for-each select="child"/>
0

Here is a stylesheet that does the transform:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" encoding="utf-8"/>

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

    <xsl:template match="Parent">
        <li>
        <xsl:value-of select="@text"/>
        <br/>
        <ul>
            <xsl:apply-templates select="child"/>
        </ul>
        </li>
    </xsl:template>

    <xsl:template match="child">
        <li><xsl:value-of select="@text"/></li>
    </xsl:template>

</xsl:stylesheet>

If you just want text output:

<?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" encoding="utf-8"/>

    <xsl:template match="Parent">
        <xsl:value-of select="@text"/>
        <xsl:text>&#xa;  </xsl:text>
        <xsl:apply-templates select="child"/>
    </xsl:template>

    <xsl:template match="child">
        <xsl:text>  </xsl:text>
        <xsl:value-of select="@text"/>
        <xsl:text>&#xa;  </xsl:text>
    </xsl:template>

</xsl:stylesheet>

1 Comment

So the correct way of doing these things is: making a template for every thing that I want to match and then calling "apply-templates"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.