Apply schema source constraints for the RTF field

I want to apply some additional xslt filtering in one Tridion schema.

I have a field named "image". This is RTF, it is configured to allow images and hyperlinks, selecting them when editing formatting functions.

The idea is to insert both the image into the RTF and make it like an RTF field.

sample Valid source:

1.<a title="google site" href="http://google.com"><img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/></a> 2.<a title="Internal link" href="tcm:202-9720"><img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/></a> 

Since RTF allows you to use a hyperlink and an image, perhaps the user can enter them in different formats. Invalid:

 1.<a title="google site" href="http://google.com"></a> <img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/> 2.<img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/> <a title="google site" href="http://google.com"></a> 3.<a title="google site" href="http://google.com"><img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/></a> <a title="Internal link" href="tcm:202-9720"><img style="width: 141px; height: 167px;" alt="Image" title="Image" src="tcm:202-11587"/></a> 

Rules:

 1.Only one Image should be allowed and it should be hyperlink. 2.There should be one <img> tag wrapped by one <a> tag. 3. Multiple images are not allowed. 

I know that this can be done using an event system. But still, I was thinking about implementation using XSLT filtering.

XSLT Filter Source:

 <xsd:element name="image" minOccurs="0" maxOccurs="1" type="tcmi:XHTML"> <xsd:annotation> <xsd:appinfo> <tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml> <tcm:Size xmlns:tcm="http://www.tridion.com/ContentManager/5.0">2</tcm:Size> <tcm:FilterXSLT xmlns:tcm="http://www.tridion.com/ContentManager/5.0"> <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform"> <output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"></output> <template match="/ | node() | @*"> <copy> <apply-templates select="node() | @*"></apply-templates> </copy> </template> <template match="*[ (self::br or self::p or self::div) and normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos; and not(@*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or @* or * or node()]) and not(following::node()[not( (self::text() or self::br or self::p or self::div) and normalize-space(translate(., &apos; &apos;, &apos;&apos;)) = &apos;&apos; and not(@*) and not(processing-instruction()) and not(comment()) and not(*[not(self::br) or @* or * or node()]) )]) ]"> <!-- ignore all paragraphs and line-breaks at the end that have nothing but (non-breaking) spaces and line breaks --> </template> <template match="br[parent::div and not(preceding-sibling::node()) and not(following-sibling::node())]"> <!-- Chrome generates <div><br/></div>. Renders differently in different browsers. Replace it with a non-breaking space --> <text> </text> </template> <template name="FormattingFeatures"> <FormattingFeatures xmlns="http://www.tridion.com/ContentManager/5.2/FormatArea" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <Doctype>Transitional</Doctype> <AccessibilityLevel>0</AccessibilityLevel> <DisallowedActions> <Bold></Bold> <Italic></Italic> <Underline></Underline> <Strikethrough></Strikethrough> <Subscript></Subscript> <Superscript></Superscript> <AlignLeft></AlignLeft> <Center></Center> <AlignRight></AlignRight> <Bullets></Bullets> <Numbering></Numbering> <IndentDecrease></IndentDecrease> <IndentIncrease></IndentIncrease> <Font></Font> <Background></Background> <InsertCharacter></InsertCharacter> <Anchor></Anchor> <Table></Table> <TableWidth></TableWidth> <TableHeight></TableHeight> <TableCellSpacing></TableCellSpacing> <TableCellPadding></TableCellPadding> <TableHAlign></TableHAlign> <TableBorderSize></TableBorderSize> <TableBorderStyle></TableBorderStyle> <TableBorderColor></TableBorderColor> <TableBackground></TableBackground> <TableCellWidth></TableCellWidth> <TableCellHeight></TableCellHeight> <TableCellHAlign></TableCellHAlign> <TableCellVAlign></TableCellVAlign> <TableCellBackground></TableCellBackground> <HLine></HLine> <SectionType></SectionType> <H1></H1> <H2></H2> <H3></H3> <H4></H4> <H5></H5> <H6></H6> <Style></Style> <Language></Language> <Abbreviation></Abbreviation> <CurrentElement></CurrentElement> </DisallowedActions> <DisallowedStyles></DisallowedStyles> </FormattingFeatures> </template> </stylesheet> </tcm:FilterXSLT> </xsd:appinfo> </xsd:annotation> </xsd:element> 

I want to know if I can do this by writing some xslt code in tcm: FilterXSLT of the schema source.

Can anyone help how to do this.

Thanks.

+4
source share
1 answer

That should work. It performs the following actions:

  • Searches for the first element 'a' in your rich text
  • if there is an "img" element inside it, it saves it as
  • If this does not happen, it searches for the first img element elsewhere in the rich text and moves it to the a element
  • all other items are deleted

Tested for 2011 sp1. And I have to say, it was fun to play with xslt again, it was too long :)

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" method="xml" cdata-section-elements="script"></xsl:output> <xsl:template match="body"> <xsl:copy> <xsl:apply-templates select="//a[1]"></xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*"></xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="//a"> <xsl:copy> <xsl:apply-templates select="@*"></xsl:apply-templates> <xsl:choose> <xsl:when test="not(img)"> <xsl:copy-of select="//img"></xsl:copy-of> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="node()"></xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:copy> </xsl:template> </xsl:stylesheet> 
+4
source

All Articles