Search

[SOLVED]

I need to perform a transformation on a content nodes html output and the Ninja technique seemed perfect for this.

I thought I had it all figured out but have noticed oddities with the 'p' tags not containing the correct child nodes and what should be inside the child nodes are actually appearing at the same level in the tree.

My DATA

<data>
<hello>paste your xml here</hello>
<content>
<p>
 <a href="#">
  <img src="http://localhost/symphony/extension/richtext_redactor/getfile/?name=f8a5d93be3a974695d3a4ca648abeec5.jpg" alt="Some text"/>
 </a>
 <img src="http://localhost/symphony/extension/richtext_redactor/getfile/?name=95dde036f82027191b93f500c7a04db3.jpg" alt="Some text"/>
</p>
</content>
</data>

My XSLT

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

<xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <hi>
      <xsl:apply-templates select="data/content/*" mode="html"/>
    </hi>
  </xsl:template>

<xsl:template match="*" mode="html">
<xsl:element name="{name()}">
    <xsl:apply-templates select="* | @* | text()" mode="html"/>
</xsl:element>
</xsl:template>

<xsl:template match="@*" mode="html">
<xsl:attribute name="{name()}">
    <xsl:value-of select="."/>
</xsl:attribute>
 </xsl:template>

<xsl:template match="p" mode="html">
 <xsl:variable name="HowMany">
 <xsl:choose>
 <xsl:when test="count(descendant::img) >= 2">
 <xsl:text>half</xsl:text>
 </xsl:when>
 <xsl:otherwise>
 <xsl:text>full</xsl:text>
 </xsl:otherwise>
 </xsl:choose>
 </xsl:variable>
 <p number="{$HowMany}">
 <xsl:apply-templates select="* | @* | text()" mode="html"/>
 </p>
 </xsl:template>  

 <xsl:template match="p//img" mode="html">
 <figure>
 <img src="{@src}"/>
 <figcaption>
  <xsl:value-of select="@alt"/>
 </figcaption>
 </figure>  
 </xsl:template>  


 </xsl:stylesheet>

Expected Output

<?xml version="1.0" encoding="UTF-8"?>
<hi>
<p number="half">
  <a href="#">
     <figure>
        <img src="http://localhost/symphony/extension/richtext_redactor/getfile/?name=f8a5d93be3a974695d3a4ca648abeec5.jpg"/>
        <figcaption>Some text</figcaption>
     </figure>
  </a>
  <figure>
     <img src="http://localhost/symphony/extension/richtext_redactor/getfile/?name=95dde036f82027191b93f500c7a04db3.jpg"/>
     <figcaption>Some text</figcaption>
  </figure>
  </p>
</hi>

Actual Output

<p class="half">
   <a href="#"></a>
 </p>
 <figure>
 <a href="#">
  <img src="http://localhost/symphony/extension/richtext_redactor/getfile/?name=f8a5d93be3a974695d3a4ca648abeec5.jpg">
              <figcaption>Some text</figcaption>
            </a>
 </figure>
<a href="#"></a>
<figure>
<img src="http://localhost/symphony/extension/richtext_redactor/getfile/?name=95dde036f82027191b93f500c7a04db3.jpg">
            <figcaption>Some text</figcaption>
          </figure>
        <p></p>

As you can see.. nothing is contained within the P tag further than one level deep..

When I use http://xslttest.appspot.com/ to parse my XSLT it works as expected, but when using Symphony it doesn't perform the same transform..

I'm using Symphony 2.6.2 at present. Any pointers?

This is a simple one - but it will involve changing your syntax.

The issue is not with your XSLT (sorry to say) but the fact that the figure element is a block element and is not allowed to reside within a p tag. So your standard browser would break it out of the paragraph tags.

Could you just double check your source code (not inspector) to confirm that it's the case.

AH! now that is where I hadn't thought to check :)

Looks like Chrome, throws elements out of sync in the browser.but when inspecting the source.. it's correct.

I changed the figure element to a span and it seems fine...

Thanks for the info Jon.

No problem you're welcome :)

Btw what I usually do (because I didn't mention) is in that case first match the image and output the figure and then the paragraph after it (in case there is more content) but it might not always be applicable.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details