Search

I have posted more details about this question (if they are helpful) at stackoverflow: http://stackoverflow.com/questions/18624925/include-html-nodes-in-xslattribute

I am essentially trying to take a <caption> from my datasource which has markdown and match it to add a class to any <a> tags that are present. Then I want to place the contents of the caption, including the tag and its new class, inside an attribute of an image.

I have been told that in order to accomplish this, I need to serialize the XML once I have processed it. I have been searching around like crazy but I cannot figure out if:

  1. I can serialize data easily in my current environment and just don't know how

  2. I can only serialize if I install saxon

  3. EXSLT offers some form of simple serialization

  4. This serialization is simple enough that I could write the logic in XSLT. I should only ever have to deal with a single link and no extra attributes except 'href'.

  5. This is too much to process in XSLT and I should be modifying my links in PHP somehow - how would this relate to a data source?

  6. I should be somehow adding my class to any links using the "unformatted" version of my <caption> - since it is markdown I have access to either "formatted" or "unformatted" versions. From what I can tell, the "unformatted" version is already serialized to the point that I can put that data into an attribute and it comes out right - I just can't add my special classes to this version.

Thanks for reading.

I don't understand what you mean by serialization, but if you want to stuff HTML/XML code inside an attribute, you can use this version of nodetostring utility.

<xsl:variable name="html-ready">
    <!-- prepare the HTML as you want it -->
</xsl:variable>

<!-- stringify it -->
<xsl:call-template name="nodetostring">
    <xsl:with-param name="node" select="$html-ready"/>
    <xsl:with-param name="esc-dblq" select="true()"/>
</xsl:template>

@vladG, you are a lifesaver! I had tried so many things, was so confused... This worked like a charm for me! I used a slightly different snippet:

<xsl:variable name="cap">
    <xsl:apply-templates select="caption/*" mode="html" />
</xsl:variable>
...
<xsl:attribute name="data-caption">
    <xsl:call-template name="nodetostring">
        <xsl:with-param name="node" select="$cap"/>
        <xsl:with-param name="esc-dblq" select="false()"/>
    </xsl:call-template>
</xsl:attribute>

Because when I set esc-dblq to true() it cancelled all of the quote marks which caused my href attribute to be appended to my site root rather than going to the full http location.

You saved me, thank you thank you.

You're welcome :)

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