创建自定义书目样式

通过了解您构建简单自定义样式所需要的步骤(和 XML 代码),在 Word 中创建自定义书目样式。 此外,还可了解创建更复杂的样式文件。 开始之前,您需要了解一些信息:

创建的书目源全部列在以下本地文件中:%AppData%\Microsoft\Bibliography\Sources.xml。

注意

在 Word 中创建第一个书目源之前,Sources.xml 文件将不存在。 所有书目样式都存储在用户的个人资料中:%AppData%\Microsoft\Bibliography\Style。

构建基本书目样式

首先,创建自定义样式将遵循的基本书目样式。

设置书目样式

若要创建书目样式,可使用您最喜爱的 XML 编辑器来创建一个 XML 样式表,也就是名为 MyBookStyle.xsl 的一个 .xsl 文件。 记事本也可以。 顾名思义,我们的示例将是一个“图书”源类型的样式。

在文件顶部添加以下代码:

<?xml version="1.0" ?> 

<!--List of the external resources that we are referencing-->
 
<xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform" xmlns:b="https://schemas.openxmlformats.org/officeDocument/2006/bibliography">
 
<!--When the bibliography or citation is in your document, it's just HTML-->
 
<xsl:output method="html" encoding="us-ascii"/>
   
<!--Match the root element, and dispatch to its children-->
   
<xsl:template match="/">

<xsl:apply-templates select="*" />

</xsl:template>

正如注释所示,Word 将使用 HTML 来表示文档内的书目或引文。 前面的大部分 XML 代码只是为样式中更有趣部分做准备。 例如,您可以给样式一个版本号来跟踪您所做的更改,如以下示例所示。

<!--Set an optional version number for this style--> 

<xsl:template match="b:version"> 

   <xsl:text>2006.5.07</xsl:text>

</xsl:template>

更重要的是,可以为您的样式命名。 添加此标记: <xsl:when test=“b:StyleNameLocalized”>;然后使用以下代码以所选语言为样式命名。

<xsl:when test="b:StyleNameLocalized/b:Lcid='1033'">

   <xsl:text>[Your Style Name]</xsl:text>
 
</xsl:when>

此节包含您的样式的区域设置名称。 在我们的示例文件中,我们想要在引用选项卡上的样式下拉列表中显示自定义书目样式名称,“简单图书样式”。若要执行此操作,添加以下 XML 代码以指定英语区域设置中的样式名称(Lcid 决定语言)。

<!--Defines the name of the style in the References dropdown list-->
<xsl:when test="b:StyleNameLocalized"> 
   <xsl:choose> 
      <xsl:when test="b:StyleNameLocalized/b:Lcid='1033'"> 
         <xsl:text>Simple Book Style</xsl:text> 
      </xsl:when> 
</xsl:when>

你的样式将显示在应用程序中书目样式下拉列表框的自己名称下。

现在,检查样式详细信息。 Word 中每一个源类型(例如图书、电影、期刊中文章等等)都有可用于书目的内置字段列表。 若要查看给定源类型的所有可用字段,在引用选项卡上,选择管理源,然后在源管理器对话框中选择新建以打开创建源对话框。 然后选择显示所有书目字段

图书源类型有以下可用字段:

  • 作者

  • 标题

  • 年份

  • 城市

  • 省/市/自治区

  • 国家/地区

  • 发行商

  • 编辑

  • 卷数

  • 翻译工具

  • 短标题

  • 标准数字

  • 页面

  • 版本

  • 备注

在代码中,可以指定对书目样式而言很重要的字段。 即使清除了显示所有书目字段,这些字段也会显示且旁边带有红色星号。 在我们的图书示例中,我想确保输入了作者、标题、年份、城市和发行商,因此我希望在这些字段旁边出现一个红色星号,以提醒用户这些是应该填写的建议字段。

<!--Specifies which fields should appear in the Create Source dialog box when in a collapsed state (The Show All Bibliography Fields check box is cleared)-->

<xsl:template match="b:GetImportantFields[b:SourceType = 'Book']"> 
   <b:ImportantFields> 
      <b:ImportantField> 
         <xsl:text>b:Author/b:Author/b:NameList</xsl:text> 
      </b:ImportantField> 
      <b:ImportantField> 
         <xsl:text>b:Title</xsl:text> 
      </b:ImportantField> 
     <b:ImportantField> 
         <xsl:text>b:Year</xsl:text> 
      </b:ImportantField> 
      <b:ImportantField> 
         <xsl:text>b:City</xsl:text>
      </b:ImportantField> 
      <b:ImportantField> 
         <xsl:text>b:Publisher</xsl:text> 
      </b:ImportantField> 
   </b:ImportantFields> 
</xsl:template>

xsl:text 标记中的 <文本> 是对 Sources.xml 文件的引用。 这些引用提取出将填充每个字段的数据。 检查 \Microsoft\Bibliography\Sources.xml 中的 Sources.xml 以了解这些引用如何与 XML 文件中的内容匹配。

设计布局

书目和引文的输出在 Word 文档中以 HTML 的形式表示,因此要定义自定义书目和引文样式在 Word 中的外观,我们必须在样式表中添加一些 HTML。

假设您想要以这种方式格式化书目中每个条目:

姓氏、名字。 (年) 。 标题。 城市:发布者

完成此任务所需的 HTML 将嵌入到样式表中,如下所示。

<!--Defines the output format for a simple Book (in the Bibliography) with important fields defined-->

<xsl:template match="b:Source[b:SourceType = 'Book']"> 

<!--Label the paragraph as an Office Bibliography paragraph-->

   <p> 
      <xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:Last"/> 
      <xsl:text>, </xsl:text> 
      <xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:First"/> 
      <xsl:text>. (</xsl:text> 
      <xsl:value-of select="b:Year"/> 
      <xsl:text>). </xsl:text> 
      <i> 
         <xsl:value-of select="b:Title"/> 
         <xsl:text>. </xsl:text> 
      </i> 
      <xsl:value-of select="b:City"/> 
      <xsl:text>: </xsl:text> 
      <xsl:value-of select="b:Publisher"/> 
      <xsl:text>.</xsl:text> 
   </p> 
</xsl:template>

当您在 Word 文档中引用图书来源时,Word 需要访问此 HTML,以便可以使用自定义样式显示源,因此必须将代码添加到自定义样式工作表,以便 Word 能够执行此操作。

<!--Defines the output of the entire Bibliography-->
 
<xsl:template match="b:Bibliography"> 

   <html xmlns="https://www.w3.org/TR/REC-html40"> 
   
      <body> 

         <xsl:apply-templates select ="b:Source[b:SourceType = 'Book']"> 

         </xsl:apply-templates> 

      </body> 
   
   </html> 
</xsl:template>

您需要以类似的方式对引文输出执行相同的操作。 遵循文档中单个引文的模式(作者、年份)。

<!--Defines the output of the Citation-->
<xsl:template match="b:Citation/b:Source[b:SourceType = 'Book']"> 
   <html xmlns="https://www.w3.org/TR/REC-html40"> 
      <body> 
         <!-- Defines the output format as (Author, Year)--> 
         <xsl:text>(</xsl:text> 
            <xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:Last"/> 
         <xsl:text>, </xsl:text> 
         <xsl:value-of select="b:Year"/> 
         <xsl:text>)</xsl:text> 
      </body> 
   </html> 
</xsl:template>

使用以下代码行关闭文件。

<xsl:template match="text()" /> </xsl:stylesheet>

将该文件另存为 MyBookStyle.XSL,并放入“样式”目录(\Microsoft\Bibliography\Style)。 重启 Word,您的样式现在出现在样式下拉列表中。 可以开始使用新的样式。

创建复杂样式

令书目样式复杂化的一个问题就是通常需要大量的条件逻辑。 例如,如果指定了日期,则需要显示日期,而如果未指定日期,则可能需要使用缩写来表示该源没有日期。

对于更具体的示例,在 APA 样式中,如果未为网站源指定日期,则缩写“n.d.”用于表示无日期,并且样式应自动执行此操作。 下面是一个示例:

未输入日期的 APA 网站源:Kwan, Y. (n.d.)。 从输入日期:Kwan, Y.(2006 年 1 月 18 日)的 https://www.microsoft.com APA 网站源检索。 从 https://www.microsoft.com 中检索

可以看到,显示的内容取决于输入的数据。

几乎每个样式的输出都需要根据你的是“企业作者”还是“普通作者”而更改。你将了解如何使用最常见的规则之一在样式中实现此类逻辑,允许在指定公司作者时显示公司作者,如果未指定公司作者,则显示普通作者。

解决方案概述

若要仅在适当的情况下才显示公司作者,请使用以下步骤。

若要显示公司作者

  1. 在代码引文部分添加一个变量以计算公司作者个数。

  2. 如果公司作者已填充,则引文中出现显示公司作者。 如果公司作者未填充,则引文中出现显示普通作者。

  3. 在代码书目部分添加一个变量以计算公司作者个数。

  4. 如果公司作者已填充,则书目中出现显示公司作者。 如果公司作者未填充,则书目中出现显示普通作者。

开始使用

我们首先来更改引文。 下面是上一次的引文代码。

<!--Defines the output of the Citation-->
<xsl:template match="b:Citation/b:Source[b:SourceType = 'Book']"> 
   <html xmlns="https://www.w3.org/TR/REC-html40"> 
      <body> 
         <!--Defines the output format as (Author, Year)-->
         <xsl:text>(</xsl:text> 
         <xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:Last"/>
         <xsl:text>, </xsl:text> 
         <xsl:value-of select="b:Year"/> 
         <xsl:text>)</xsl:text> 
      </body>
   </html> 
</xsl:template>

步骤 1: 在引文部分定义一个新变量以计算公司作者个数

声明一个新变量,以帮助确定是否有可用公司作者。 该变量是源中公司作者字段存在次数的计数。

<!--Defines the output of the Citation-->
<html xmlns="https://www.w3.org/TR/REC-html40">
   <!--Count the number of Corporate Authors (can only be 0 or 1)-->
      <xsl:variable name="cCorporateAuthors"> 
         <xsl:value-of select="count(b:Author/b:Author/b:Corporate)" /> 
      </xsl:variable>

步骤 2: 验证是否已填充了公司作者

验证是否已填充了公司作者。 您可以通过确定企业作者的数量为非零来实现这一点。 如果公司作者存在,将其显示出来。 如果不存在,则显示普通作者。


<xsl:text>(</xsl:text> 
<xsl:choose>
<!--When the corporate author exists, display the corporate author-->
<xsl:when test ="$cCorporateAuthors!=0"> 
<xsl:value-of select="b:Author/b:Author/b:Corporate"/> 
</xsl:when>
<!-- When the corporate author does not exist, display the normal author--> 
<xsl:otherwise> 
<xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:Last"/> 
</xsl:otherwise> 
</xsl:choose> 
<xsl:text>, </xsl:text>

既然已经更改了引文,随后更改书目。 以下是本文前面的书目部分。

<!--Defines the output format for a simple Book (in the Bibliography) with important fields defined-->
<xsl: template match="b:Source[b:SourceType = 'Book']">
<!--Label the paragraph as an Office Bibliography paragraph--> 
<p> 
<xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:Last"/> 
<xsl:text>, </xsl:text> 
<xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:First"/> 
<xsl:text>. (</xsl:text> 
<xsl:value-of select="b:Year"/> 
<xsl:text>). </xsl:text> 
<i>

步骤 3: 在书目部分定义一个新变量

同样,我们首先来添加一个计数变量。

<!--Defines the output format for a simple Book (in the Bibliography) with important fields defined-->
<xsl: template match="b:Source[b:SourceType = 'Book']"> 
<!--Count the number of Corporate Authors (can only be 0 or 1)-->
<xsl:variable name="cCorporateAuthors"> 
<xsl:value-of select="count(b:Author/b:Author/b:Corporate)" /> 
</xsl:variable>

步骤 4: 验证是否已填充了公司作者

验证已存在公司作者。

…..
<xsl:variable name="cCorporateAuthors"> 
<xsl:value-of select="count(b:Author/b:Author/b:Corporate)" /> 
</xsl:variable> 
<p> 
<xsl:choose>
<!--When the corporate author exists display the corporate author-->
<xsl:when test ="$cCorporateAuthors!=0"> 
<xsl:value-of select="b:Author/b:Author/b:Corporate"/> 
<xsl:text>. (</xsl:text> 
</xsl:when> 
<xsl:otherwise> 
<!--When the corporate author does not exist, display the normal author-->
<xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:Last"/> 
<xsl:text>, </xsl:text> 
<xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:First"/> 
<xsl:text>. (</xsl:text>
</xsl:otherwise> 
</xsl:choose>

此处是完整最终代码。

<?xml version="1.0" ?> 
<!--List of the external resources that we are referencing-->
<xsl:stylesheet version="1.0" xmlns:xsl="https://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:b="https://schemas.openxmlformats.org/officeDocument/2006/bibliography">
   <!--When the bibliography or citation is in your document, it's just HTML-->
   <xsl:output method="html" encoding="us-ascii"/> 
   <!--Match the root element, and dispatch to its children-->
   <xsl:template match="/"> 
      <xsl:apply-templates select="*" /> 
   </xsl:template>
   <!--Set an optional version number for this style-->
   <xsl:template match="b:version"> 
      <xsl:text>2006.5.07</xsl:text> 
   </xsl:template> 
   <!--Defines the name of the style in the References dropdown-->
   <xsl:template match="b:StyleName">     
      <xsl:text>Simple Book Style</xsl:text> 
   </xsl:template> 
   <!--Specifies which fields should appear in the Create Source dialog when in a collapsed state (The Show All Bibliography Fieldscheckbox is cleared)-->
   <xsl:template match="b:GetImportantFields[b:SourceType = 'Book']"> 
      <b:ImportantFields> 
         <b:ImportantField><xsl:text>b:Author/b:Author/b:NameList</xsl:text> </b:ImportantField> 
         <b:ImportantField> <xsl:text>b:Title</xsl:text> </b:ImportantField> 
         <b:ImportantField> <xsl:text>b:Year</xsl:text> </b:ImportantField> 
         <b:ImportantField> <xsl:text>b:City</xsl:text> </b:ImportantField> 
         <b:ImportantField> <xsl:text>b:Publisher</xsl:text> </b:ImportantField> 
      </b:ImportantFields> 
   </xsl:template>
   <!--Defines the output format for a simple Book (in the Bibliography) with important fields defined-->
   <xsl:template match="b:Source[b:SourceType = 'Book']">
   <!--Count the number of Corporate Authors (can only be 0 or 1-->
   <xsl:variable name="cCorporateAuthors">
      <xsl:value-of select="count(b:Author/b:Author/b:Corporate)" />
   </xsl:variable>
   <!--Label the paragraph as an Office Bibliography paragraph-->
   <p>
      <xsl:choose>
         <xsl:when test ="$cCorporateAuthors!=0">
         <!--When the corporate author exists display the corporate author-->
            <xsl:value-of select="b:Author/b:Author/b:Corporate"/>
            <xsl:text>. (</xsl:text>
         </xsl:when>
         <xsl:otherwise>
            <!--When the corporate author does not exist, display the normal author-->
            <xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:Last"/>
            <xsl:text>, </xsl:text>
            <xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:First"/>
            <xsl:text>. (</xsl:text>
         </xsl:otherwise>
      </xsl:choose>
      <xsl:value-of select="b:Year"/>
      <xsl:text>). </xsl:text>
      <i>
         <xsl:value-of select="b:Title"/>
         <xsl:text>. </xsl:text>
      </i> 
         <xsl:value-of select="b:City"/>
         <xsl:text>: </xsl:text>
         <xsl:value-of select="b:Publisher"/>
         <xsl:text>.</xsl:text>
      </p>
   </xsl:template>
   <!--Defines the output of the entire Bibliography-->
   <xsl:template match="b:Bibliography"> 
      <html xmlns="https://www.w3.org/TR/REC-html40"> 
         <body>
            <xsl:apply-templates select ="*">
            </xsl:apply-templates>
         </body>
      </html>
   </xsl:template>
   <!--Defines the output of the Citation-->
   <xsl:template match="b:Citation/b:Source[b:SourceType = 'Book']">
      <html xmlns="https://www.w3.org/TR/REC-html40"> 
         <xsl:variable name="cCorporateAuthors"> 
            <xsl:value-of select="count(b:Author/b:Author/b:Corporate)" /> 
         </xsl:variable> 
         <body> 
         <!--Defines the output format as (Author, Year--> 
            <xsl:text>(</xsl:text>
            <xsl:choose> 
            <!--When the corporate author exists display the corporate author-->
               <xsl:when test ="$cCorporateAuthors!=0">
                  <xsl:value-of select="b:Author/b:Author/b:Corporate"/>
               </xsl:when>
               <!--When the corporate author does not exist, display the normal author-->
               <xsl:otherwise> 
                  <xsl:value-of select="b:Author/b:Author/b:NameList/b:Person/b:Last"/> 
               </xsl:otherwise>
               </xsl:choose>
               <xsl:text>, </xsl:text> 
               <xsl:value-of select="b:Year"/>
               <xsl:text>)</xsl:text> 
            </body> 
         </html>
   </xsl:template>
   <xsl:template match="text()" />
</xsl:stylesheet>

结束语

本文展示了如何在 Word 中创建自定义书目样式。首先创建一个简单的样式,然后使用条件语句创建一个更复杂的样式。

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。