Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sean 141 posts 179 karma points
    Feb 12, 2011 @ 04:51
    Sean
    0

    adding a custom title field into the document template

    Hi There,

    I have a macro that renders Keywords and descriptions on my template. I want to know if I can add a title tag into it as well, but I'm unsure of the syntax. Can someone help me with the XSLT and the XPATH for this please?

    I'm using:

    unbraco 4.6, windows 2008 & .net 4.0.

    Thanks in advance for your answer

    Sean

     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ &lt;!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="msxml umbraco.library">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
      <xsl:variable name="title" select="/macro/title"/>
      <xsl:variable name="keywords" select="/macro/keywords"/>
    <xsl:variable name="description" select="/macro/description"/>
      <xsl:template match="/">
      <title><xsl:value-of select="{$title}"/></title>
      <meta name="keywords" content="{$keywords}" />
    <meta name="description" content="{$description}" />
    </xsl:template>
    </xsl:stylesheet>

     

     <umbraco:Item field="metaDescription" insertTextBefore="&lt;meta name=&quot;description&quot; content=&quot;" insertTextAfter="&quot; /&gt;" runat="server"></umbraco:Item>
       <umbraco:Item field="metaKeywords" insertTextBefore="&lt;meta name=&quot;keywords&quot; content=&quot;" insertTextAfter="&quot; /&gt;" runat="server"></umbraco:Item>

     

  • Tobias Morf 80 posts 183 karma points
    Feb 12, 2011 @ 09:28
    Tobias Morf
    0

    Your code looks perfect.

    But I don't understand why you posted two differtent solutions (xslt and page fields).

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 12, 2011 @ 15:56
    Kim Andersen
    0

    Hi Sean

    Have you inserted the XSLT macro inside the head-section of your page? Or are you using the umbraco:Item's to render the meta-tags?

    This part:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ &lt;!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="msxml umbraco.library">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
      <xsl:variable name="title" select="/macro/title"/>
      <xsl:variable name="keywords" select="/macro/keywords"/>
    <xsl:variable name="description" select="/macro/description"/>
      <xsl:template match="/">
      <title><xsl:value-of select="{$title}"/></title>
      <meta name="keywords" content="{$keywords}" />
    <meta name="description" content="{$description}" />
    </xsl:template>
    </xsl:stylesheet>

    should wotk if you have inserted the macro inside the template.

    But this part:

    <umbraco:Item field="metaDescription" insertTextBefore="&lt;meta name=&quot;description&quot; content=&quot;" insertTextAfter="&quot; /&gt;" runat="server"></umbraco:Item>
    <umbraco:Item field="metaKeywords" insertTextBefore="&lt;meta name=&quot;keywords&quot; content=&quot;" insertTextAfter="&quot; /&gt;" runat="server"></umbraco:Item>

    would only generate the description and keywords meta-tags if you insert these in the template.

    /Kim A

  • Sean 141 posts 179 karma points
    Feb 12, 2011 @ 23:00
    Sean
    0

    Hi All,

    Using the XSLT macro above.

    1. I created 3 fields in the document type keywords, description and title.
    2. I then added the macro into my template  <umbraco:Macro Alias="CreateMetatags" runat="server"></umbraco:Macro>
    3. I put some content in each of these fields and then published the document.

    Now I get the error below:

    Error reading XSLT file: \xslt\seanMetaTags.xslt

    Any ideas what i'm missing?

    Sean

  • Lesley 284 posts 143 karma points
    Feb 12, 2011 @ 23:36
    Lesley
    0

    Hi Sean,

    Go to your xslt file in the Developer section of umbraco and click the "visualise xslt" button. Choose a page on your site where you have entered the title fields etc. Does the visualisation give the expected output? If there are problems with the xslt, you will get an error with more detail here.

  • Sean 141 posts 179 karma points
    Feb 13, 2011 @ 01:02
    Sean
    0

    Hi Lesley,

    Here is the output from the visual. What am I missing?

    Error parsing the XSLT:

    System.Xml.Xsl.XslLoadException: XSLT compile error. An error occurred at (3,3). ---> System.Xml.XmlException: Expected DTD markup was not found. Line 3, position 3. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.DtdParserProxy.System.Xml.IDtdParserAdapter.Throw(Exception e) at System.Xml.DtdParser.Throw(Int32 curPos, String res, String arg) at System.Xml.DtdParser.ScanSubsetContent() at System.Xml.DtdParser.GetToken(Boolean needWhiteSpace) at System.Xml.DtdParser.ParseSubset() at System.Xml.DtdParser.ParseInDocumentDtd(Boolean saveInternalSubset) at System.Xml.DtdParser.Parse(Boolean saveInternalSubset) at System.Xml.DtdParser.System.Xml.IDtdParser.ParseInternalDtd(IDtdParserAdapter adapter, Boolean saveInternalSubset) at System.Xml.XmlTextReaderImpl.ParseDtd() at System.Xml.XmlTextReaderImpl.ParseDoctypeDecl() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at System.Xml.Xsl.Xslt.XsltInput.ReadTextNodes() at System.Xml.Xsl.Xslt.XsltInput.ReadNextSibling() at System.Xml.Xsl.Xslt.XsltInput.FindStylesheetElement() at System.Xml.Xsl.Xslt.XsltLoader.LoadDocument() at System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include) --- End of inner exception stack trace --- at System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include) at System.Xml.Xsl.Xslt.XsltLoader.Load(XmlReader reader) at System.Xml.Xsl.Xslt.XsltLoader.Load(Compiler compiler, Object stylesheet, XmlResolver xmlResolver) at System.Xml.Xsl.Xslt.Compiler.Compile(Object stylesheet, XmlResolver xmlResolver, QilExpression& qil) at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) at umbraco.macro.CreateXsltTransform(XmlTextReader xslReader, Boolean debugMode) at umbraco.presentation.umbraco.developer.Xslt.xsltVisualize.visualizeDo_Click(Object sender, EventArgs e)

    -
  • Lesley 284 posts 143 karma points
    Feb 13, 2011 @ 11:09
    Lesley
    0

    Sean,

    Can you check line 2. Do you really have

    <!DOCTYPE xsl:stylesheet [ &lt;!ENTITY nbsp "&#x00A0;"> ]>

    or perhaps it should be

    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>

    (Note the &lt;)

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 13, 2011 @ 16:04
    Kim Andersen
    0

    Hi again Sean.

    I'd check the thing that Lesley mentioned as well, to see if the error disappears.

    Beside that, if you have created three fields on your document types for each of the three meta tags, then you should probably change this code:

    <xsl:variable name="title" select="/macro/title"/>
    <xsl:variable name="keywords" select="/macro/keywords"/>
    <xsl:variable name="description" select="/macro/description"/>

    to something like this:

    <xsl:variable name="title" select="$currentPage/title"/>
    <xsl:variable name="keywords" select="$currentPage/keywords"/>
    <xsl:variable name="description" select="$currentPage/description"/>

    /Kim A

  • Sean 141 posts 179 karma points
    Feb 15, 2011 @ 11:25
    Sean
    0

    Hi Guys,

    Thanks very much for the help. I played around with the code, taking all suggestions on ob board. I finally got it to work after having a little trouble with the title.

    I Appreciate your help.

    Sean

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="msxml umbraco.library">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:variable name="title" select="$currentPage/title"/>
    <xsl:variable name="keywords" select="$currentPage/keywords"/>
    <xsl:variable name="description" select="$currentPage/description"/>
    <xsl:variable name="googlekey" select="$currentPage/googlekey"/>      
          
      <xsl:template match="/">
    <title><xsl:value-of select="$currentPage/title"/></title>
      <meta name="keywords" content="{$keywords}" />
    <meta name="description" content="{$description}" />
        <meta name="verify-v1" content="{$googlekey}" />
    </xsl:template>
    </xsl:stylesheet>

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 15, 2011 @ 13:12
    Kim Andersen
    0

    Great to hear Sean.

    Just a quick note: you could consider using the $title instead of $currentPage/title in the title-element, or just skip the creation of the title-variable. It's just a small thing, but just wanted to let you know :)

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft