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:
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 ?
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
Check this blog post, might be a solution to your problem here.
Cheers,
/Dirk
I've got this same problem.. but the link to the blog doesn't exist!
What was the solution?
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
Site seems to be coming back, altho no styles yet, but you'll be able to see the code of the post.
/Dirk
Internal server error from here.. will give it a few mins
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>
Are you using an xslt extension here.. or is it build in?
An extension, but build in with umbraco:)
so if u are using the default umbraco empty xslt file. it works
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>
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.
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:
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?
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
That shows an empty text area! :(
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 " "> ]>
<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="<Empty />";
}
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>
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!
is working on a reply...