2009-09-19 178 views
3

定期出现的标签我有一个xml文档,我正在使用xslt转换为xsl-fo文档。我有这个棘手的问题我一直在试图寻找一个解决方案,很长一段时间......XSLT转换替换通过文档

在我的源XML我有整个穿插了少量的标签。我想将它们格式化为生成文档中的下划线,但是我无法这样做。

我用这样的代码尝试:

<xsl:template match="//em"> 
    <fo:inline text-decoration="underline"> 
    <xsl:apply-templates select="*|text()"/> 
    </fo:inline> 
</xsl:template> 

完整的XSLT看起来是这样的:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    version="1.0"> 



    <!-- match em tags --> 
    <xsl:template match="//em"> 
    <fo:inline text-decoration="underline"> 
     <xsl:apply-templates select="*|text()"/> 
    </fo:inline> 
    </xsl:template> 
    <xsl:template match="//u"> 
    <fo:inline text-decoration="underline"> 
     <xsl:apply-templates select="*|text()"/> 
    </fo:inline> 
    </xsl:template> 

    <!-- match b tags --> 
    <xsl:template match="//b"> 
    <fo:inline font-weight="bold"> 
     <xsl:apply-templates select="*|text()"/> 
    </fo:inline> 
    </xsl:template> 

    <xsl:template match="//br"> 
    <fo:block><xsl:text>&#xA;</xsl:text></fo:block> 
    </xsl:template> 

    <xsl:template match="briefs"> 
    <fo:root> 
     <fo:layout-master-set> 
     <fo:simple-page-master master-name="Evidence" page-width="8.5in" page-height="11in" margin="1in"> 
      <fo:region-body margin-bottom=".5in" margin-top=".5in" region-name="xsl-region-body" /> 
      <fo:region-before extent="1em" region-name="xsl-region-before" /> 
      <fo:region-after extent="1em" region-name="xsl-region-after" /> 
     </fo:simple-page-master> 
     </fo:layout-master-set> 
     <xsl:for-each select="brief"> 
     <fo:page-sequence master-reference="Evidence"> 
     <fo:static-content flow-name="xsl-region-before" font-family="Times"> 
      <fo:block font-size="10pt" text-align="center" color="#666666"> 
      <fo:inline font-style="italic"><xsl:value-of select="title"/></fo:inline> by <xsl:value-of select="author"/> 
      </fo:block> 
     </fo:static-content> 

     <fo:static-content flow-name="xsl-region-after" font-family="Times" font-size="10pt"> 
      <fo:table> 
      <fo:table-column /> 
      <fo:table-column column-width="1in" /> 
      <fo:table-body> 
       <fo:table-row> 
       <fo:table-cell> 
        <fo:block text-align="left" color="#666"><xsl:value-of select="copyright"/></fo:block> 
       </fo:table-cell> 
       <fo:table-cell> 
        <fo:block text-align="right" font-weight="bold"> 
        Page <fo:page-number/> 
        </fo:block> 
       </fo:table-cell> 
       </fo:table-row> 
      </fo:table-body> 
      </fo:table> 
     </fo:static-content> 

     <fo:flow flow-name="xsl-region-body" font-family="Times"> 
      <fo:block font-size="14pt" text-align="center" text-transform="uppercase" border-before-width="2pt" border-before-color="black" border-before-style="double" border-after-width="1pt" border-after-color="black" border-after-style="solid" background-color="#ccc"> 
      <xsl:value-of select="title"/> 
      </fo:block> 

      <xsl:for-each select="heading"> 
      <xsl:choose> 
       <xsl:when test="@level = 2"> 
       <fo:block font-size="11pt" font-weight="bold" keep-with-next="always" text-transform="uppercase" padding-before="1em"> 
        <xsl:value-of select="title"/></fo:block> 
       </xsl:when> 
       <xsl:when test="@level = 3"> 
       <fo:block font-size="10pt" font-weight="normal" keep-with-next="always" text-transform="uppercase" padding-before="1em"> 
        <xsl:value-of select="title"/></fo:block> 
       </xsl:when> 
       <xsl:otherwise> 
       <fo:block font-size="12pt" font-weight="bold" keep-with-next="always" text-transform="uppercase" padding-before="1em"> 
        <xsl:value-of select="title"/></fo:block> 
       </xsl:otherwise> 
      </xsl:choose> 

      <xsl:for-each select="content/item"> 
       <xsl:choose> 
       <xsl:when test="@type = 'card'"> 
        <!--Print the taglines--> 
        <fo:block font-size="10pt" font-weight="bold" padding-before="1em" keep-with-next="always"> 
        <!--<xsl:number value="position()" format="1" />. --> 
        <xsl:value-of select="tagline"/> 
        </fo:block> 

        <!--Print the citation--> 
        <fo:block font-size="10pt" font-style="italic" keep-with-next="always" keep-together.within-page="always" margin-left=".25in" padding-before=".5em"> 
        <!--<xsl:number value="position()" format="1" />. --> 
        <xsl:value-of select="citation" disable-output-escaping="yes" /> 
        </fo:block> 

        <!--Print the body--> 
        <fo:block font-size="10pt" keep-together.within-page="always" margin-left=".25in" padding-before=".5em"> 
        <!--<xsl:number value="position()" format="1" />. --> 
        <xsl:value-of select="quote" disable-output-escaping="yes" /> 
        </fo:block> 

       </xsl:when> 
       <xsl:otherwise> 
        <fo:block font-size="10pt" padding-before=".5em"><xsl:value-of select="."/></fo:block> 
       </xsl:otherwise> 
       </xsl:choose> 
      </xsl:for-each> 

      </xsl:for-each> 


     </fo:flow> 

     </fo:page-sequence> 
     </xsl:for-each> 
    </fo:root> 
    </xsl:template> 

</xsl:stylesheet> 

有没有人有什么想法? 非常感谢!!!!!

+0

你能发表你的意见样本吗? – lavinio 2009-09-29 01:56:38

回答

7

我不清楚你的代码在哪里实际上与文档“疯狂” - 即运行匹配。如果<briefs>是根节点,那么我希望在某个点看到一些<xsl:apply-templates select="*"/>或类似;否则你是只有去得到在根匹配中指定的输出(并且它永远不会应用你的<em>等比赛)。

如果你想定义只有在使用替代来处理整个文档,那么经典的比赛是一样的东西:

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

因为这所有的节点,和瀑布相匹配,它运行的每一个节点,直到更多找到特定的匹配项。所以用XSLT:

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

大多数节点将被重复“原样”,但任何<bar>元件将与<BAR/>被替换(并且没有任何属性/内容)。

请注意,即使有这个;你需要需要显式级联,如果你期望子数据被匹配处理(如上面的<bar>示例所示,其中不是级联)。在<briefs>比赛中我看不到任何<xsl:apply-templates>

最后; match="//em"是多余的; match="em"应该就足够了。

1

没有看到整个样式没有帮助,但是匹配语法可能应该是match="em"match="//em"

+0

感谢您的回答!是的,我也尝试过这种方式,并没有帮助... – markwatson 2009-09-26 00:30:47

0

看起来好像你在做XSLT而不是声明性的。 MG是正确的,我想你会有更多的运气,如果你做少“for-each”-ing和更多“apply-templates”-ing。

看起来您并未匹配您的所有em节点,因为您未在主根模板中应用模板。我很想说,每个你有一个for-each的地方,你应该使用apply-templates来代替,但这有点学术。