4

我想在一个 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 代码来做到这一点。

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

谢谢。

4

1 回答 1

4

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

  • 在富文本中查找第一个“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>
于 2012-07-03T10:12:43.593 回答