Forum Moderators: open

Message Too Old, No Replies

Checking for and adding to XHTML style attribute

XML tag may have 0-4 attributes to put into an xhtml tags style

         

charles95621

10:56 pm on May 15, 2007 (gmt 0)

10+ Year Member



I have a problem that seemed simple at first simple but is giving me some headaches in implementation.

Here is the problem at a high level. Based upon an XML tag I must create an XHTML div. This div has a class with default styling but the user wants to be able to alter this styling but has only a limited knowledge of CSS. The only attributes she would alter is text color, background color, border color, top margin, and bottom margin.

The logic would be something like

If attributes are listed
for each attribute
if style attribute for the div exists
append appropriate CSS property and value to the style attribute
else
create a style attribute for the div with property and value

cmarshall

1:49 am on May 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This can be easily done with XSLT. You could probably also do it by messing with a DOM object, but XSLT would be cleaner.

charles95621

3:15 am on May 16, 2007 (gmt 0)

10+ Year Member



I am doing this with XSLT but the code I was using generates an error. Here is one of several trys. I know why this fails, the style attribute should be first but it does reflect some early thinking.

<xsl:if test="@Background">
<xsl:choose>
<xsl:when test="@style">
<xsl:value-of select="concat(@style,'background-color=',@Background,';')"/>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="style">
<xsl:value-of select="concat('background-color=',@Background,';')"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="@Border">
<xsl:choose>
<xsl:when test="@style">
<xsl:value-of select="concat(@style,'border-color=',@Border,';')"/>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="style">
<xsl:value-of select="concat('border-color=',@Border,';')"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="@Font">
<xsl:choose>
<xsl:when test="@style">
<xsl:value-of select="concat(@style,'color=',@Font,';')"/>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="style">
<xsl:value-of select="concat('color=',@Font,';')"/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:if>

A little more current thinking say that I should check the style first and nest the if inside but it occured to me that if I did that the attribute is not yet set so the appending to it would be a problem throug the remaining if statements.

cmarshall

1:29 pm on May 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll look this over in a bit.

By the way, you need to disable smilies in your post.

charles95621

7:01 am on May 17, 2007 (gmt 0)

10+ Year Member



I have been doing some experiments with this using variables. This will work fine if the variable is set but breaks if the attribute that contains what to add is a null value.

cmarshall

3:23 am on May 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry I haven't gotten back to you on this. It's been a busy week, and I haven't checked in here too often.

Still not ready to look at it, but I didn't want you to feel neglected (this is a quiet forum).

charles95621

6:09 am on May 18, 2007 (gmt 0)

10+ Year Member



Thanks I am trying some things myself. I am doing some reading and doing some experiments. No luck thus far.

charles95621

12:45 pm on May 20, 2007 (gmt 0)

10+ Year Member



I have been working on this and I think I am very close. This is what I have come up with so far

/*****************************************************/
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns="http://www.w3.org/1999/xhtml"
>

<xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="HighlightBox">
<xsl:variable name="styleString">
<xsl:element name="TempStyle">
<xsl:for-each select="@*">
<xsl:element name="StyleTMargin">
<xsl:if test="local-name()='topmargin'">
<xsl:value-of
select="concat('margin-top:',.,';')"/>
</xsl:if>
</xsl:element>
<xsl:element name="StyleBMargin">
<xsl:if test="local-name()='bottom-margin'">
<xsl:value-of
select="concat('margin-bottom:',.,';')"/>
</xsl:if>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:variable>
<div>
<xsl:attribute name="style">
<xsl:value-of
select="concat(msxsl:nod-set($styleString)/TempStyle/StyleTMargin,msxsl:node-set($styleString)/TempStyle/StyleBMargin)"/>
</xsl:attribute>
</div>
</xsl:template>

</xsl:stylesheet>
/*******************************************************/
The problem is that I cant use the node-set function. I get the folowing error

/*******************************************************/
System.Xml.Xsl.XslTransformException was unhandled by user code
Message="Cannot find the script or external object that implements prefix 'urn:schemas-microsoft-com:xslt'."
Source="System.Data.SqlXml"
LineNumber=0
LinePosition=0
StackTrace:
at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
at System.Xml.Xsl.CompiledQuery.Query.<xsl:template match="HighlightBox">(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, Double {urn:schemas-microsoft-com:xslt-debug}position, Double {urn:schemas-microsoft-com:xslt-debug}last, IList`1 {urn:schemas-microsoft-com:xslt-debug}namespaces) in C:\Users\crussell\Documents\Visual Studio 2005\WebSites\JanetReedPublications\MultiAtt.xsl:line 32
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates>(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates>(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates>(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates>(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates>(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.<xsl:apply-templates>(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator , Double , Double )
at System.Xml.Xsl.CompiledQuery.Query.Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.CompiledQuery.Query.Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
at System.Xml.Xsl.XmlILCommand.Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
at System.Xml.Xsl.XslCompiledTransform.Transform(XmlReader input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver)
at Microsoft.Xsl.XslPlugin.Execute(String xml, Stream output, XmlOutputMethod& outputMethod)
/*****************************************

I feel sure that I am very close but not there yet

cmarshall

1:19 pm on May 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm still pretty tied up right now (I spent almost the entire day yesterday reinstalling my OS -fun).

I'll SM you something that I use to parse Microsoft Excel XML. It may be very apropos to what you're doing.