我正在寻找一种方法,将任何被引用的图/节点复制到它被引用的地方。
<chapter id="intro">
<title>Introduction</title>
<para>Welcome to our new product. One of its
new features is a <xref linkend="some-figure"/>.
Other new features include ...
</para>
<para>Grab that <xref linkend="some-figure"/> and pull it here too.</para>
</chapter>
<chapter>
<title>Some other chapter</title>
<para>This chapter contains the figure!
<figure id="some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>
</para>
</chapter>
这可以变成:吗
<chapter id="intro">
<title>Introduction</title>
<para><figure><title>my figure...</title><mediaobject>...</mediaobject></figure>
Welcome to our new product. One of its
new features is a <xref linkend="some-figure"/>.
Other new features include ...
</para>
<para><figure><title>my figure...</title><mediaobject>...</mediaobject></figure>
Grab that <xref linkend="some-figure"/> and pull it here too.
</para>
</chapter>
<chapter>
<title>Some other chapter</title>
<para>This chapter contains the figure!
<figure id="some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>
</para>
</chapter>
更新引用以指向复制的图形将是锦上添花,但我想要有关信息以及将节点复制到引用位置的方法。
因此,给定任何包含xref
的para
,您需要将链接图形(减去其id
属性)复制到para
内容的开头。我会定义一个键,以便通过id:快速访问figure
元素
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="figureById" match="figure" use="@id" />
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
<xsl:template match="para[.//xref]">
<xsl:copy>
<xsl:apply-templates select="@*" /><!-- may not be necessary -->
<!-- copy in referenced figures -->
<xsl:apply-templates select="key('figureById', .//xref/@linkend)"
mode="noid"/>
<!-- and continue with child nodes as normal -->
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<!-- special almost-identity template to remove the id attribute -->
<xsl:template match="node()" mode="noid">
<xsl:copy>
<xsl:apply-templates select="@*[local-name() != 'id']|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这利用了key
函数的一个很好的特性,即如果将节点集作为其第二个参数(要查找的键值)传递给它,则结果是节点集的并集,该并集是依次查找每个键值的结果。因此,key('figureById', xref/@linkend)
为您提供了所有图形元素,其id
与本段中xref
元素的任何匹配,但如果同一图形被多次引用,则只能插入图形的一个副本。
更新参考资料以指向复制的数字将是锦上添花的
您可以通过重写复制图形的ID以包括目标段落的generate-id()
,然后在xref
链接端使用相同的转换来实现这一点。类似这样的东西:
<xsl:template match="para[.//xref]">
<xsl:copy>
<xsl:apply-templates select="@*" /><!-- may not be necessary -->
<!-- copy in referenced figures, modifying their ids to include our own -->
<xsl:apply-templates select="key('figureById', .//xref/@linkend)"
mode="modify-id">
<xsl:with-param name="targetNode" select="." />
</xsl:apply-templates>
<!-- and continue with child nodes as normal -->
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="node()" mode="modify-id">
<xsl:param name="targetNode" />
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="id">
<xsl:value-of select="concat(generate-id($targetNode), '-', @id)" />
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<!-- munge linkend attributes in the same way we did for copied figure ids -->
<xsl:template match="para//xref/@linkend">
<xsl:attribute name="linkend">
<xsl:value-of select="concat(generate-id(ancestor::para[1]), '-', .)" />
</xsl:attribute>
</xsl:template>
在我的系统中,使用xsltproc
(并将您的示例包装在根标记中以使其形成良好的格式),这将生成
<?xml version="1.0"?>
<root>
<chapter id="intro">
<title>Introduction</title>
<para><figure id="idp1744-some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>Welcome to our new product. One of its
new features is a <xref linkend="idp1744-some-figure"/>.
Other new features include ...
</para>
<para><figure id="idp2656-some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>Grab that <xref linkend="idp2656-some-figure"/> and pull it here too.</para>
</chapter>
<chapter>
<title>Some other chapter</title>
<para>This chapter contains the figure!
<figure id="some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>
</para>
</chapter>
</root>
生成的ID的确切形式(本例中为idpNNNN
)因处理器而异,但它们保证是唯一和一致的。
如果您只是想用相应的figure
替换xref
,您可以将@linkend
与@id
进行匹配。
示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="xref">
<xsl:apply-templates select="//figure[@id=current()/@linkend]"/>
</xsl:template>
</xsl:stylesheet>