Copied to clipboard

Flag this post as spam?

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


  • Chris White 23 posts 43 karma points
    Jul 12, 2010 @ 11:00
    Chris White
    0

    Parse a property value as XML

    Hi all,

    I have a string property into which I store an XML document. Within my XSLT I can select this property as follows:

    <xsl-value-of select="$currentPage/thePropertyName" />

    Which seems to work fine and brings back the contents of the property (as a string I assume). However when I want to select something from inside this XML string it doesn't appear to work:

    <xsl-value-of select="$currentPage/thePropertyName/someElement/@someAttribute" />

    I'm assuming this doesn't work because the XSLT sees the value as a string and hence can't perform XPath against it ? Is there an umbraco.library function that I can use to rectify this and return the value as a parseable XML string, or will I need to write my own XSLT extension to handle this, and if so does anyone have any pointers ?

    Thanks in advance,

    Chris

  • Dirk De Grave 4534 posts 5981 karma points MVP 3x admin c-trib
    Jul 12, 2010 @ 11:16
    Dirk De Grave
    0

    Check this blog post, might be a solution to your problem here.

     

    Cheers,

    /Dirk

  • Bex 444 posts 555 karma points
    Sep 06, 2010 @ 13:42
    Bex
    0

    I've got this same problem.. but the link to the blog doesn't exist!

    What was the solution?

  • Dirk De Grave 4534 posts 5981 karma points MVP 3x admin c-trib
    Sep 06, 2010 @ 13:43
    Dirk De Grave
    0

    Hmm, don't know by heart actually, but I'll tweet the author to get his site back up for the solution to be available to you....

     

    Cheers,

    /Dirk

  • Dirk De Grave 4534 posts 5981 karma points MVP 3x admin c-trib
    Sep 06, 2010 @ 13:44
    Dirk De Grave
    0

    Site seems to be coming back, altho no styles yet, but you'll be able to see the code of the post.

     

    /Dirk

  • Bex 444 posts 555 karma points
    Sep 06, 2010 @ 13:46
    Bex
    0

    Internal server error from here.. will give it a few mins

  • Hendrik Jan 71 posts 137 karma points
    Sep 06, 2010 @ 13:55
    Hendrik Jan
    0

     

    Hey, blog doesnt work it.

    here is some of my sample code, just convert the string with xml to a msxml:node-set

    <xsl:variable name="xmlString">
      <xml>
        <node></node>
        <node><node></node><node></node></node>
        <node></node>
      </xml>
    </xsl:variable>
    <xsl:variable name="xml" select="msxml:node-set($xmlString)/xml"></xsl:variable>
  • Bex 444 posts 555 karma points
    Sep 06, 2010 @ 13:59
    Bex
    0

    Are you using an xslt extension here.. or is it build in?

  • Hendrik Jan 71 posts 137 karma points
    Sep 06, 2010 @ 14:00
    Hendrik Jan
    0

    An extension, but build in with umbraco:)

    so if u are using the default umbraco empty xslt file. it works

  • Bex 444 posts 555 karma points
    Sep 06, 2010 @ 14:16
    Bex
    0

    hmm doesn't seem to work for me.

    I assume that your variable xmlstring can also be from a property?

    I have the following:

     <xsl:param name="currentPage"/>
    <xsl:variable name="IssuuXml" select="$currentPage/data [@alias = 'DocumentUpload']"/>
    <xsl:variable name="myxml" select="msxml:node-set($IssuuXml)/xml"/>

    and then

     <xsl:value-of select="$myxml"/>

    so I can view the xml..

    The xml I am using is in this format: (but ti think its getting encoded somewhere long the way as my <> and lts and gt etc..

    <main><UniqueName value="5639f055c66d4e78876b5b4f14c57e62" /><DocumentId value="100906113115-8cfb9e2332ff44db8ff77eac6b1d36d4" /><Title value="tet" /><Description value="dfgdfgdfg" /><CommentAllowed value="false" /><Downloadable value="false" /><Access value="public" /></main>

  • Hendrik Jan 71 posts 137 karma points
    Sep 06, 2010 @ 14:21
    Hendrik Jan
    0

     

    Sorry, use:

    <xsl:variable name="myxml" select="msxml:node-set($IssuuXml)/*"/>

    (note the *, i used xml. U should use main as that is your root element. Or * that works always.

  • Bex 444 posts 555 karma points
    Sep 06, 2010 @ 14:34
    Bex
    0

    It doesn't seem to be working here, my <xsl:value-of select="$myxml"/> doesn;t seem to bring back anything.
    in the db it's stored as follows:

    <

     

     

    data alias="DocumentUpload"><![CDATA[<Main><UniqueName value="cdd5cd7d1eb64dc99b0e3ac2208d21d9" /><DocumentId value="100906111129-a955f72e93a74046bde3ad6fa80b0be8" /><Title value="gfgfhgfhgf" /><Description value="hgfhgfhfgh" /><CommentAllowed value="false" /><Downloadable value="false" /><Access value="public" /></Main>]]></data>

    My whole xslt file is as follows if it helps:

    <?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" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:TagCloud.Helper="urn:TagCloud.Helper" xmlns:ecommerce="urn:ecommerce"
     exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets TagCloud.Helper ecommerce ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:variable name="IssuuXml" select="$currentPage/data [@alias = 'DocumentUpload']"/>
    <xsl:variable name="myxml" select="msxml:node-set($IssuuXml)/*"/>
    <xsl:template match="/">
    <xsl:value-of select="$myxml"/>
    </xsl:template>
    </xsl:stylesheet> 

     

    Do I have anything missing?

    Although that said.. Itried adding exactly what you had above

    <xsl:variable name="xmlString">
      <xml>
        <node></node>
        <node><node></node><node></node></node>
        <node></node>
      </xml>
    </xsl:variable>
    <xsl:variable name="xml" select="msxml:node-set($xmlString)/xml"></xsl:variable>

    and it was still blank? Iam currently using umbraco 4.0.3 as its quite an old version I am updating.. should this make a difference?


     

  • Hendrik Jan 71 posts 137 karma points
    Sep 06, 2010 @ 14:39
    Hendrik Jan
    0

    Updating doesn't make a diff i think.

    Have u tried:

    <textarea>
      <xsl:copy-of select="$myxml"/>
    </textarea>

    value-of isnt gonna work here :P

  • Bex 444 posts 555 karma points
    Sep 06, 2010 @ 14:42
    Bex
    0

    That shows an empty text area! :(

  • Hendrik Jan 71 posts 137 karma points
    Sep 06, 2010 @ 15:03
    Hendrik Jan
    1

    Oh, ok well i looked up one of my old xslt files for you, this wil work. 

    <?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:cd="urn:schemas-connect-digital"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" 
      exclude-result-prefixes="cd msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <xsl:variable name="xml" select="cd:parse($currentPage/data [@alias = 'DocumentUpload'])/*"/>

    <textarea>
      <xsl:copy-of select="$xml"/>
    </textarea>

    </xsl:template>
        
        
        
    <!--  convert string to xml -->   
    <msxml:script language="CSharp" implements-prefix="cd">
      <msxml:using namespace="System.IO" />
      
      public XPathNodeIterator parse(string data) {
      
          if(data==null || data.Length==0) {
              data="&lt;Empty /&gt;";
        }
      
          StringReader stringReader = new StringReader(data);
          XPathDocument xPathDocument = new XPathDocument(stringReader);
          XPathNavigator xPathNavigator = xPathDocument.CreateNavigator();
        XPathExpression xPathExpression = xPathNavigator.Compile("/");
      
          XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPathExpression);
          
        return xPathNodeIterator;
      }
    </msxml:script>

    </xsl:stylesheet>

     

     

  • Bex 444 posts 555 karma points
    Sep 06, 2010 @ 15:15
    Bex
    0

    That works.. although I was hoping to avoid using c# if possible.. but never mind if it works it work!

    Thank you for spending your time helping me!

Please Sign in or register to post replies

Write your reply to:

Draft