storing umbraco:item fields into the xsl:variable - xslt

I want to use creationDate field in my template in umbraco.
I know that i can extract this field with this
<umbraco:Item field="createDate" runat="server" />
but I want to store this data as a xsl:variable and manipulate it.
How can I do it?

All of a node's attributes and properties are automatically available in your XSLT macros through a variable called currentPage. So, in your macro simply use the following line:
<xsl:variable name="myDate" select="$currentPage/#createDate" />
Should you wish to then format the date, use the function provided by the umbraco.library extension, like so:
<xsl:value-of select="umbraco.library:FormatDateTime($myDate, 'dd-MM-yyyy')" />
If you need more precise manipulation over your date var, look into the Exslt.ExsltDatesAndTimes extension that comes packaged with Umbraco. There's about forty-odd functions in there, too.

Related

How to select a property of a macro parameter in XSLT?

This is a very basic XSL question and I am just getting started with the topic in the context of Umbraco:
I've defined a macro with a XSLT file. The macro has a parameter of type 'ContentPicker'. What I want to do with the macro, is to render the picked content in a certain way. The relevant bit of my XSLT file is this:
<xsl:param name="source" select="/macro/BlogPostSource"/>
<xsl:template match="/">
<div>
<xsl:value-of select="$source/blogPostIntroduction"/>
</div>
I define a parameter, which is set to the parameter of the macro (this works). Now I simply want to render the property blogPostIntroduction which is a generic property on the picked content. This doesn't work. If I use
<xsl:value-of select="$source"/>
the ID of the content is rendered.
Question A: How do I select fields of the selected content?
Question B: Is my idea correct in general or am I missing a better way to do what I need, rather than using macros and XSLT?

Retrieving sub-element attribute's value based on uniq-key with xslt ant

My xml looks something like this:
<someRoot
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../my.xsd">
<position name="quality">
<db>
<server name="blah-blah" type="xyz"/>
</db>
<some-more-element value="12345"/>
<my-needy-element>
<my-needy-sub-element name="uk1"
value="a,b,c"
more-attr="some-val"/>
<my-needy-sub-element name="uk2"
value="x,b,y,c"
more-attr="some-diff-val"/>
</my-needy-element>
</position>
</someRoot>
Retrieving say my single db-server name works after I do xmlproperty & samy something like : ${db.server(name)}. Now I am trying to retrieve value for sub elements - but it always
gives me all row values matching it. eg: ${my-needy-element.my-needy-sub-element(more-attr)} will print some-val,some-diff-val . How do I get attribute value matching
to paricular unique name , something like {my-needy-element.my-needy-sub-element(more-attr) with name="uk1"}
Adding editing comment: Is there any way even if I use xslt ant-task by passing 'uk1' & get required sub-element attribute value? with xsl:if or key match
Amending more:
Have tried with xsl & then an xslt ant , but it doesnot seem to give what I am looking for:
<xsl:template match="/someRoot/my-needy-element/my-needy-sub-element">
<someRoot>
<xsl:key name="my-needy-sub-element" match="text()" use="#name"/>
<xsl:apply-templates select="key('name',$xslParamPassed)"/>
</someRoot>
</xsl:template>
thanks,
You can't do this with xmlproperty but you might consider the third-party xmltask which lets you use full-fledged XPath expressions:
<xmltask source="file.xml">
<copy path="/someRoot/position/my-needy-element/my-needy-sub-element[#name='uk1']/#more-attr"
property="more.attr" />
</xmltask>
<echo>${more.attr}</echo>

Using a parameter passed into xslt stylesheet

I am using Saxon to perform a transformation of an XML document in my .NET application. I am passing in a parameter to my xslt document but I have no idea how to use it in my template.
Here is what I have done so far:
var zipcode = _db.AXCustomers.FirstOrDefault(x => x.ACCOUNTNUM == accNo).ZIPCODE;
transformer.SetParameter(new QName("CustomerZipCode"), new XdmAtomicValue(zipcode));
Then in my xslt document I am specifying the parameter like so:
<xsl:template match="/">
<xsl:param name="CustomerZipCode" />
But when I try to use the parameter, nothing appears. I am using it like so:
<xsl:value-of select="substring-before($CustomerZipCode, ' ')"/>
But nothing is output even though my zipcode does contain a value
You are using xsl:param inside a xsl:template element, it means that the param is for the template. The parameter you are passing from the .net code is a transformer parameter and related xsl:param must be placed at the top level of the stylesheet, into the xsl:stylesheet element.

How to get template name inside template

Is it possible to get template name while inside template?
Example:
<xsl:template name="list">
<!-- Get name value (in this case "list") -->
</xsl:template>
As far as I am aware, neither XSLT 1.0 nor XSLT 2.0 offer such a feature.
As Martin says, there's probably no way to do this.
Even if such a capacity existed, surely using it couldn't be any simpler than just defining a variable with the value you want:
<xsl:template name="list">
<xsl:variable name="class" select="'list'" />
and then using that variable where you need it. This also has the benefit that you can change the template name without rewriting all your CSS, or change the class name without rewriting your XSLT.
One of the benefits of the fact that XSLT uses XML syntax is that it's easy to transform stylesheets. Modifying every named template to contain a variable holding the template name is dead easy.
#JLRishe's answer is a good approach. However, there is a trap. Any <xsl:param> must be declared before any <xsl:variable>.
This will not work:
<xsl:template name="foobarsnafu">
<xsl:variable name="foo" value="bar">
<xsl:param name="snafu"/>
But this will:
<xsl:template name="foobarsnafu">
<xsl:param name="snafu"/>
<xsl:variable name="foo" value="bar">
This might be a problem if, for example, you want the default value of your snafu <xsl:param> to be the value of your foo <xsl:variable>. The way around it would be to use <xsl:param> with a default value for your template name, i.e.:
<xsl:template name="foobarsnafu">
<xsl:param name="template_name" select="foobarsnafu"/>

XSLT parameter is always empty <xsl:param name="mediaFolderId" select="/macro/mediaFolderId" />

I created an xslt called GraphicsRowSlider with the following parameters
<xsl:param name="mediaFolderId" select="/macro/mediaFolderId" />
<xsl:param name="title" select="/macro/title" />
And added the parameters title and mediaFolderId to the related macro.
I then created new Macro Container data type and selected the macro GraphicsRowSlider as allowed macro. I then added a new field of the new data type into a document type and then imported that field into a template.
Finally, from the content, I inserted the macro and added a title and selected media folder... However, I could see that the macro calls the correct xslt with the correct title and mediaFolderId but the parameters are always empty!
Any thought?! Note, I always get this
<?UMBRACO_MACRO macroalias="GraphicsRowSlider" title="Add Title here" mediaFolderId="1159" />
The syntax you are using suggests that you are using an old umbraco version, which one are you using?
The definition how to work with macro parameters can be found on the umbraco wiki: http://our.umbraco.org/wiki/reference/templates/umbracomacro-element/macro-parameters
hth
Your input is an attribute (at least your xml example suggests this) so your xpath should look like this:
<xsl:param name="mediaFolderId" select="/macro/#mediaFolderId" />
<xsl:param name="title" select="/macro/#title" />