Xslt 如何匹配xsl fo生成的元素?

Xslt 如何匹配xsl fo生成的元素?,xslt,xslt-2.0,xsl-fo,Xslt,Xslt 2.0,Xsl Fo,我有一个.xsl文件,如下所示: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:exslt="htt

我有一个
.xsl
文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:exslt="http://exslt.org/common">
  <xsl:template match="/>
    <fo:root>
      <fo:block>...</fo:block>
    </fo:root>
  </xsl:template>
</xsl:stylesheet>
我找到并尝试了一些类似于

  <xsl:template match="/>
    <xsl:variable name="foRoot">
      <fo:root>
        <fo:block>...</fo:block>
      </fo:root>
    </xsl:variable>
    <xsl:apply-templates select="exslt:node-set($foRoot)" />
  </xsl:template>

如果您真的使用XSLT2处理器,那么首先您不需要
exsl:node set

至于你的模板

<xsl:template match="fo:table-cell">
  <xsl:attribute name="background-color">red</xsl:attribute>
</xsl:template>
这会将属性添加到元素的浅层副本中,然后使用
apply templates
保持子元素的活动处理

当然,您还需要添加标识转换模板

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>


确保不希望更改的图元已被复制。如果您使用的其他模板干扰了标识转换,则可能需要使用模式来分隔处理步骤。

谢谢!这就像一个符咒(即使它最终像地狱一样冗长…)。
<xsl:copy-of select="$foRoot" />
<xsl:template match="fo:table-cell">
  <xsl:attribute name="background-color">red</xsl:attribute>
</xsl:template>
<xsl:template match="fo:table-cell">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:attribute name="background-color">red</xsl:attribute>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>