【问题标题】:Manipulate XSLT file in C#在 C# 中操作 XSLT 文件
【发布时间】:2018-12-19 14:42:52
【问题描述】:

我有一个使用 XSLT 文件生成 PDF 文件的程序,XSLT 文件中的一些节点有一个属性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">

【问题讨论】:

  • 您是否尝试过使用the overloads of the Load method 之一,它允许您加载 XSLT 样式表?
  • 不,我没有,你的意思是 Load(XmlReader stylesheet) 吗?通过xml阅读器加载XSLT然后更新属性?
  • 嗯,XSLT 是 XML,XSLT 是一种转换 XML 的转换语言,因此您可以肯定地使用 XSLT 将其他一些 XSLT 转换为第三个 XSLT。另一方面,根据您的需要,使用例如声明全局参数就足够了。 &lt;xsl:param name="font-family"&gt;Arial&lt;/xsl:param&gt; 然后您可以在代码中的其他地方引用它,例如&lt;fo:block font-family="{$font-family}"&gt;...&lt;/fo:block&gt;,然后使用 .NET API 将 XsltArgumentList 传递给 Transform 方法。
  • @MartinHonnen,谢谢您的回答,我是 XSLT 的新手,我不知道如何才能获得第三个 xslt,我无法声明参数,因为保存了一些 xslt在数据库中,它们取决于客户端(我无法访问客户端数据库)。
  • 您能否编辑您的问题以显示 XSLT 示例。不一定是整个 XSLT,而是当前设置了 font-family 的位(即您要更改的位)。谢谢!

标签: c# xslt xpath


【解决方案1】:

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 文档包含一些具有 font-familyfont-size 属性的 XSL-FO 元素,并将这些元素转换为从全局参数中获取值。

网上的例子是https://xsltfiddle.liberty-development.net/94hvTAo

在 .NET API 中,您可以在使用 Transform 方法 (https://docs.microsoft.com/en-us/dotnet/api/system.xml.xsl.xslcompiledtransform.transform?view=netframework-4.7.2) 运行 XSLT 时使用 XsltArgumentList (https://docs.microsoft.com/en-us/dotnet/api/system.xml.xsl.xsltargumentlist?view=netframework-4.7.2) 设置此类全局参数。

【讨论】:

    【解决方案2】:

    为什么不向样式表添加另一个参数?

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

    ...

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

    【讨论】:

      猜你喜欢
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2016-05-28
      • 2014-11-26
      相关资源
      最近更新 更多