【问题标题】:Applying the restrictions on the schema source for an RTF field对 RTF 字段的架构源应用限制
【发布时间】:2012-07-03 12:20:11
【问题描述】:

我想在一个 Tridion Schema 中应用一些额外的过滤 xslt。

我有一个名为“图像”的字段。它是一个 RTF,它已被配置为通过在编辑格式功能下选择它来允许图像和超链接。

其背后的想法是在RTF中插入和Image,使其成为RTF字段。

样本有效来源:

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>

由于 RTF 允许超链接和图像,因此用户可能会以不同的格式输入它们。 无效:

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>

规则:

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.

我知道这可以使用事件系统来完成。但我仍然想通过应用 XSLT 过滤来实现。

XSLT 过滤来源:

<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>

我想知道我是否可以通过在架构源的 tcm:FilterXSLT 中编写一些 xslt 代码来做到这一点。

任何人都可以帮助如何做到这一点。

谢谢。

【问题讨论】:

  • 将组件链接视为一种替代方法,因为它提供对有效模式和包装链接的标记的控制(通过模板代码)。 &lt;a&gt; 应该指向什么?您可以在另一个组件、多媒体元数据或可能使用自定义链接类型 GUI 扩展中管理该超链接(和其他元数据)。

标签: xslt tridion


【解决方案1】:

这应该可行。它执行以下操作:

  • 在富文本中查找第一个“a”元素
  • 如果其中有一个“img”元素,它会保持原样
  • 如果没有,它会在富文本中的其他任何位置查找第一个 img 元素并将其移动到 a 元素中
  • 所有其他元素都被删除

在 2011 sp1 上测试。而且我不得不说,再次玩 xslt 很有趣,时间太长了:)

<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>

【讨论】:

  • 我在 XSLT 过滤下添加了它,但它没有按预期工作。我只是想知道,如何处理多个图像字段,如果没有标签,它的行为方式。因为我试过了,它不能正常工作。
  • 您应该将此视为示例实现,其中包含我指出的业务规则。如果您必须实施不同的业务规则,您至少可以以此为起点。一点 XSLT 知识在这里有很长的路要走..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 2015-12-15
相关资源
最近更新 更多