1

So I want to make an XSLT that would transform any XML following the rule:

<root1>
   <elem1>text</elem1>
   <elem2>text</elem2>
   ...
   <elemx>text</elemx>
   ... 
   <image>someimgreference</image
   <linkref>alink</linkref>
</root1>

<root2>
   <elem1>text</elem1>
   <elem2>text</elem2>
   ...
   <elemx>text</elemx>
   ... 
   <image>someimgreference</image
   <linkref>alink</linkref>
</root2>

using the XSLT I would get a table with the titles and when I have text, I only show the text, when I have image link I get the actual image with

<xsl:template match="image">
    <img src="{@url}" />
</xsl:template>

or something and when I have a linkref, I just post the click-able link

root1/root2 is just an exemple... those could be any names, same goes for elem1/2/3/... I want the xslt to work with any XML with that structure. So no element name references.

HTML table would look like:

    elem1 | elem2 |... |image|linkref
  ------------------------------------
    text  | text  |... |photo|link
    text  | text  |... |photo|link

I found some good references online but none really helped me get the table or none were generic enough. Is it possible what I want? Or how close can I get to getting this generic xslt?

This is the most generic I've found. I just need the image links to be shows as actual photos and link references to click-able links

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

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

<xsl:template match="/*">
<TABLE BORDER="1">
<TR>
        <xsl:for-each select="*[position() = 1]/*">
          <TD>
              <xsl:value-of select="local-name()"/>
          </TD>
        </xsl:for-each>
</TR>
      <xsl:apply-templates/>
</TABLE>
</xsl:template>

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

<xsl:template match="/*/*/*">
<TD>
    <xsl:value-of select="."/>
</TD>
</xsl:template>
</xsl:stylesheet>

I don't know where to insert the image template or the link templates (or how to write the link template) I tried inserting it like a normal template and it doesn't work. It won't do anything with the link. Not to mention the clickable one... Any ideas? At all?

5
  • Have you tried any sample xslt? Can you please post? Commented Apr 4, 2016 at 13:35
  • I edited and added the generic code to transform any xml in a table... I just need the image links to be transformed into photos and link references to be transformed in click-able links Commented Apr 4, 2016 at 13:42
  • Any ideas? I added all the code I could think of Commented Apr 4, 2016 at 16:49
  • Well, it is bit pointless to show XSLT with templates matching three levels deep if the input sample you show does not even have three levels of elements. And your attempt <xsl:template match="image"> <img src="{@url}" /> </xsl:template> looks good as long as the image elements have a url attribute, again your input sample does not show any such attribute. Commented Apr 4, 2016 at 18:16
  • It would be something like <xsl:template match="image"> <img src="{.}" /> </xsl:template> Wouldn't this work? And the xml could have 3 levels, god knows. Worst case scenario. But I can't fit the image tamplate anywhere to make it work Commented Apr 4, 2016 at 18:18

1 Answer 1

2

You've got the right template to use in comments, however you probably need to add a priority attribute to it to ensure it gets matched before the one matching /*/*/*

<xsl:template match="image" priority="2">
   <td>
    <img src="{.}" /> 
   </td>
</xsl:template>

Alternatively, to save repeatedly outputting td tags for each template, you could output in the template that matches tr before applying the templates to match the child nodes

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/">
<HTML>
<BODY>
    <xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="/*">
<TABLE BORDER="1">
<TR>
        <xsl:for-each select="*[position() = 1]/*">
          <TD>
              <xsl:value-of select="local-name()"/>
          </TD>
        </xsl:for-each>
</TR>
      <xsl:apply-templates/>
</TABLE>
</xsl:template>

<xsl:template match="/*/*">
<TR>
    <xsl:for-each select="*">
      <td>
        <xsl:apply-templates select="." />
      </td>
  </xsl:for-each>
</TR>
</xsl:template>

<xsl:template match="image">
   <img src="{.}" /> 
</xsl:template>

<xsl:template match="linkref">
  <a href="{.}">
    <xsl:value-of select="." />
  </a>
</xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

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.