代码之家  ›  专栏  ›  技术社区  ›  Bilel Chaouadi

在C中操作XSLT文件#

  •  0
  • Bilel Chaouadi  · 技术社区  · 7 年前

    font-family 我们希望在配置文件中配置字体,这意味着我们希望在应用xslt样式之前修改所有属性的值:

    public void GeneratePDF(string xsltPath, DataSet data)
    {
      XslCompiledTransform xslTrans = new XslCompiledTransform();
    
      // here I want modify all elements having the font-family attribute
       xslTrans.Load(path)
       //....
    }
    

    XSLT文件如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    
      <xsl:import href="../masterLayout.xslt" />
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:param name="ServerAppPath" select="'c:\inetpub\wwwroot\webApi'"/>
      <xsl:param name="Orientation" select="'Portrait'"/>
      <xsl:param name="PaperSize" select="'A4'"/>   
      <xsl:template match="StatusA">
            <fo:root>
                <xsl:call-template name="FOFileHeader"/>
    
                <!-- DOCUMENT -->
                <fo:page-sequence master-reference="pages" font-family="Verdana" initial-page-number="1" font-size="8pt">
                    <xsl:call-template name="Region1"/>
                    <xsl:call-template name="Region2"/>
                    <xsl:call-template name="FirstRegionAfter"/>
                    <fo:flow flow-name="xsl-region-body" font-size="8pt">
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Martin Honnen    7 年前

    XSLT是XML,因此可以使用XSLT将一个XSLT样式表转换为另一个样式表,下面是一个简单的示例

    <xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fo="http://www.w3.org/1999/XSL/Format"
        version="1.0">
    
      <xsl:param name="font-family">Times New Roman</xsl:param>
      <xsl:param name="font-size">16pt</xsl:param>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="fo:*/@font-family">
          <xsl:attribute name="font-family">
              <xsl:value-of select="$font-family"/>
          </xsl:attribute>
      </xsl:template>
    
      <xsl:template match="fo:*/@font-size">
          <xsl:attribute name="font-size">
              <xsl:value-of select="$font-size"/>
          </xsl:attribute>
      </xsl:template>
    
    </xsl:stylesheet>
    

    作为它的输入,它期望任何XML或XSLT文档包含一些XSL-FO元素 font-family font-size 属性并将其转换为从全局参数获取值。

    一个在线例子是 https://xsltfiddle.liberty-development.net/94hvTAo

    在.NET API中,您可以使用 XsltArgumentList ( https://docs.microsoft.com/en-us/dotnet/api/system.xml.xsl.xsltargumentlist?view=netframework-4.7.2 Transform 方法( https://docs.microsoft.com/en-us/dotnet/api/system.xml.xsl.xslcompiledtransform.transform?view=netframework-4.7.2

        2
  •  0
  •   Michael Kay    7 年前

    为什么不在样式表中添加另一个参数呢?

     <xsl:param name="FontFamilty" select="'Verdana'"/>
    

    ...

     <fo:page-sequence master-reference="pages" font-family="{$FontFamily}"...