?
Solved

Change font and skip lines in XSL

Posted on 2009-02-16
9
Medium Priority
?
1,065 Views
Last Modified: 2013-11-19

Hi, I'm new to XSL transformations so bear with me.  I've got an xsl stylesheet where I'm trying to skip lines after each XML item called 'message'.  I haven't figured out where to put my <br/> tags, and I also want to be able to change the font of my xml element node 'date' to a different font color.  I'm brand new so I appreciate any help
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
 <xsl:output method="xml" indent="yes"/>
     
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
      <table width="500" border="0">
        <xsl:for-each select="Message">
          <tr>
            <td>
              <xsl:value-of select="message" />
            
            </td>
            </tr>
         
          <br/>
        </xsl:for-each>
      </table>
   
       </xsl:template>
</xsl:stylesheet>

Open in new window

0
Comment
Question by:JeffEae
  • 6
  • 3
9 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23649402
This can not be very usefull, You generate a table for every element in the tree, and for every attribute.
Can you send in a piece of the source XML please?

You will need to add something like this

  <xsl:template match="message">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    <br />
</xsl:template>
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23649414
It would be very nice if you would send a small piece of the XML and a small piece of the HTML you expect from it
0
 

Author Comment

by:JeffEae
ID: 23649555
Sorry about that.  Here's the XML
<?xml version="1.0" encoding="utf-8"?>
<Message>
  <Message>
    <title>First Message</title>
    <subtitle>First</subtitle>
    <date>2/4/2009</date>
    <messagetext>Hello World</messagetext>
  </Message>
  <Message>
    <title>Hey</title>
    <subtitle>Make more sales</subtitle>
    <date>2/12/2009</date>
    <messagetext>Sending message</messagetext>
  </Message>
</Message>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23649579
Do you want this in a table?
What is the HTML you want with this.
If you put it in a table it will come out just fine
0
 

Author Comment

by:JeffEae
ID: 23649590
I guess I want each 'Message'  in its own table with red font for the date element and each table to skip a line.  
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23649626
Try this to start with and see wheither it comes close to what you need.
The predicates in the match attributes are to make distinction between Message elements that have a Message children an dthose that have a Message parent

If you control the XML, change it to using <Messages> as a container instead of <Message>
The current model sucks
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="Message[Message]">
        <html>
            <body>
                <table border="1">
                    <xsl:apply-templates select="Message"/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Message[parent::Message]">
        <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="subtitle"/></td>
            <td style="color:blue;"><xsl:value-of select="date"/></td>
            <td><xsl:value-of select="messagetext"/></td>
        </tr>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 23649654
Ah, I see,
more something like this maybe?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="Message[Message]">
        <html>
            <body>
                    <xsl:apply-templates select="Message"/>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="Message[parent::Message]">
        <table border="1">
            <xsl:apply-templates select="*"></xsl:apply-templates>
        </table>
        <br />
    </xsl:template>
    <xsl:template match="title | subtitle | messagetext">
        <tr>
            <th><xsl:value-of select="name()"/></th>
            <td><xsl:value-of select="."/></td>
        </tr>
    </xsl:template>
    <xsl:template match="date">
        <tr>
            <th><xsl:value-of select="name()"/></th>
            <td style="color:red;"><xsl:value-of select="."/></td>
        </tr>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

0
 

Author Closing Comment

by:JeffEae
ID: 31547296
Appreciate your patience and promptness!
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 23649914
welcome
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

809 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