Inserting XSLT Function inside Class="" or Title="" ??

Topics: Community, Troubleshooting, Visual editor, XSLT
Jun 25, 2012 at 10:08 PM
Edited Jun 25, 2012 at 10:11 PM

How can I insert an XSLT function inside a Class="" or Title="" attribute within a page template?

For example I have this piece of code:

<div class="ui-icon" title=""></div>

and I have a function looking like this:

<f:function name="Xyz.Functions.Fn_SettingsValue">
    <f:param name="Field" value="cae645a2-2691-4fcb-99d8-cc29b5df6462" />
</f:function>

I have numerous places where I would like to place 'input parameter XSLT functions' into page templates basically like this:

<div class="ui-icon" title="<f:function name="MyRentals.Functions.Fn_SettingsValue"> <f:param name="Field" value="cae645a2-2691-4fcb-99d8-cc29b5df6462" /> </f:function>"></div>

Where the function returns a string inside the quote marks at render time?
The above fails validation, am I missing a symbol which would allow me to embed this function into the space? 

I was hoping to be able to use the same Function in many places across the site where the div's and span's around the function are very different.

Jun 25, 2012 at 10:18 PM

i think with xslt you're supposed to do this

<div class="ui-icon">
   <xsl:attribute name="title">
      <f:function name="MyRentals.Functions.Fn_SettingsValue">
          <f:param name="Field" value="cae645a2-2691-4fcb-99d8-cc29b5df6462" />
       </f:function>">
   <xsl:attribute>
</div>

Jun 25, 2012 at 10:21 PM

if your other-function is ie. Razor, you can insert the title-value like this

<div class="ui-icon" title="@Html.C1().Function("MyRentals.Functions.Fn_SettingsValue", new { Field = Guid.Parse("cae645a2-2691-4fcb-99d8-cc29b5df6462") })"></div>

Coordinator
Jun 27, 2012 at 8:06 AM

the only way to dynamically add an attribute in an xml page template is to call a function that returns XAttribue.

Since Xslt functions can return only XElement/XhmltDocument you would have to write a C# function that does it.

<div class="ui-icon">
      <f:function name="MyRentals.Functions.Fn_SettingsValue">
          <f:param name="Field" value="cae645a2-2691-4fcb-99d8-cc29b5df6462" />
       </f:function>">
</div>

Alternatively you can make an Xslt function that inserts the whole tag, and not only attribute, that would bethe easiest way to do it.
We have alternatives to xml page templates, ASP.NET master pages supported by @burningice's CompositeC1Contrib project, and C1 4.0 will have Razor based page templates.

Coordinator
Jun 27, 2012 at 8:28 PM

If you like XSLT and prefer to use this you could turn things a bit around and instead of calling your function inside the html element which you wish to modify, you call it outside. The two models are shown below.

<!-- instead of this -->
<div>
  <!-- calling C# function that return a custom XAttribute - this gets attached to the outer div here -->
  <f:function name="XAttribute.Producing.Function" />
  ... more stuff
</div>

<!-- you can call an XSLT Function -->
<!-- Copy all input xml from param XElementToModify, adding custom attribute to the root element -->
<f:function name="XAttribute.Producing.Function">
  <f:param name="XElementToModify">
    <div>
      ... more stuff
    </div>
  </f:param>
<f:function>

You could also consider basing your page templates on Razor like @burningice suggest - this environment allows for more flexibility.