使用XSL向XML子元素添加文本,同时保留子元素的所有子元素和属性
我试图根据段落的“步骤”父级的“性能”属性,在段落中添加一些文本 如果某个步骤标记为“performance=”optional“,我希望生成的文本(对于下面的第二个步骤)如下所示: “2.(可选)这是第2步…”使用XSL向XML子元素添加文本,同时保留子元素的所有子元素和属性,xml,xslt,docbook,Xml,Xslt,Docbook,我试图根据段落的“步骤”父级的“性能”属性,在段落中添加一些文本 如果某个步骤标记为“performance=”optional“,我希望生成的文本(对于下面的第二个步骤)如下所示: “2.(可选)这是第2步…” 这是步骤1,这是必需的。 这是第2步,它是可选的,不同于。 我不想在转换过程中丢失这张纸条。 这是第三步。 我尝试使用Xpath匹配我的节点并对其进行修改:,然后使用concat()尝试预先添加可选文本,但我会丢失外部参照链接(可能是因为concat()不尊重子项和属性?) 使用下
这是步骤1,这是必需的。
这是第2步,它是可选的,不同于。
我不想在转换过程中丢失这张纸条。
这是第三步。
我尝试使用Xpath匹配我的节点并对其进行修改:
,然后使用concat()尝试预先添加可选文本,但我会丢失外部参照链接(可能是因为concat()不尊重子项和属性?)
使用下面的xsl定制,我已经接近我想要的了,但是(可选)文本位于段落之外,这会将步骤文本放在一行中,有时会与页面内容中断。我真正想要的是将生成的文本放在第一段中
有人有什么建议吗
<!-- Add "(Optional) " to steps that have the performance="optional" attribute set -->
<xsl:template match="procedure/step|substeps/step">
<xsl:variable name="id">
<xsl:call-template name="object.id"/>
</xsl:variable>
<xsl:variable name="keep.together">
<xsl:call-template name="pi.dbfo_keep-together"/>
</xsl:variable>
<fo:list-item xsl:use-attribute-sets="list.item.spacing">
<xsl:if test="$keep.together != ''">
<xsl:attribute name="keep-together.within-column"><xsl:value-of
select="$keep.together"/></xsl:attribute>
</xsl:if>
<fo:list-item-label end-indent="label-end()">
<fo:block id="{$id}">
<!-- dwc: fix for one step procedures. Use a bullet if there's no step 2 -->
<xsl:choose>
<xsl:when test="count(../step) = 1">
<xsl:text>•</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="." mode="number">
<xsl:with-param name="recursive" select="0"/>
</xsl:apply-templates>.
</xsl:otherwise>
</xsl:choose>
</fo:block>
</fo:list-item-label>
<xsl:choose>
<xsl:when test="@performance='optional'">
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:text>(Optional) </xsl:text>
<xsl:apply-templates/>
</fo:block>
</fo:list-item-body>
</xsl:when>
<xsl:otherwise>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates/>
</fo:block>
</fo:list-item-body>
</xsl:otherwise>
</xsl:choose>
</fo:list-item>
</xsl:template>
•;
.
(可选)
从您的问题中不清楚您只是想知道如何添加“(可选)”文本,还是想将其合并到XSL-FO中,或者是否还想使用“2.”编号。对于第一种情况,这里有一个简单的答案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="step[@performance = 'optional']/para/text()[1]">
<xsl:value-of select="concat('(Optional) ', normalize-space())"/>
</xsl:template>
</xsl:stylesheet>
在样例输入上运行时,将生成:
<procedure>
<step id="step_lkq_c1l_5j">
<para>This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>(Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>This is step 3.</para>
</step>
</procedure>
<procedure>
<step id="step_lkq_c1l_5j">
<para>This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>2. (Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>This is step 3.</para>
</step>
</procedure>
<procedure>
<step id="step_lkq_c1l_5j">
<para>1. This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>2. (Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>3. This is step 3.</para>
</step>
</procedure>
这是步骤1,这是必需的。
(可选)这是第2步,它是可选的,不同于。
我不想在转换过程中丢失这张纸条。
这是第三步。
要获取编号,可以使用以下内容替换第二个模板:
<xsl:template match="step[@performance = 'optional']/para/text()[1]">
<xsl:variable name="sequence">
<xsl:number level="multiple" count="step"/>
</xsl:variable>
<xsl:value-of select="concat($sequence, '. (Optional) ', normalize-space())"/>
</xsl:template>
<xsl:template match="step/para/text()[1]">
<xsl:variable name="sequence">
<xsl:number level="multiple" count="step"/>
</xsl:variable>
<xsl:value-of select="concat($sequence, '. ')"/>
<xsl:if test="../../@performance = 'optional'">
<xsl:text>(Optional) </xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space()"/>
</xsl:template>
产生:
<procedure>
<step id="step_lkq_c1l_5j">
<para>This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>(Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>This is step 3.</para>
</step>
</procedure>
<procedure>
<step id="step_lkq_c1l_5j">
<para>This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>2. (Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>This is step 3.</para>
</step>
</procedure>
<procedure>
<step id="step_lkq_c1l_5j">
<para>1. This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>2. (Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>3. This is step 3.</para>
</step>
</procedure>
这是步骤1,这是必需的。
2.(可选)这是第2步,它是可选的,不同于。
我不想在转换过程中丢失这张纸条。
这是第三步。
要对所有步骤进行编号,您可以将同一模板替换为:
<xsl:template match="step[@performance = 'optional']/para/text()[1]">
<xsl:variable name="sequence">
<xsl:number level="multiple" count="step"/>
</xsl:variable>
<xsl:value-of select="concat($sequence, '. (Optional) ', normalize-space())"/>
</xsl:template>
<xsl:template match="step/para/text()[1]">
<xsl:variable name="sequence">
<xsl:number level="multiple" count="step"/>
</xsl:variable>
<xsl:value-of select="concat($sequence, '. ')"/>
<xsl:if test="../../@performance = 'optional'">
<xsl:text>(Optional) </xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space()"/>
</xsl:template>
(可选)
产生:
<procedure>
<step id="step_lkq_c1l_5j">
<para>This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>(Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>This is step 3.</para>
</step>
</procedure>
<procedure>
<step id="step_lkq_c1l_5j">
<para>This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>2. (Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>This is step 3.</para>
</step>
</procedure>
<procedure>
<step id="step_lkq_c1l_5j">
<para>1. This is step 1, which is required.</para>
</step>
<step performance="optional">
<para>2. (Optional) This is step 2, which is optional, unlike<xref linkend="step_lkq_c1l_5j" />.
<note><para>I don't want to lose this note in my transformation.</para></note></para>
</step>
<step>
<para>3. This is step 3.</para>
</step>
</procedure>
1.这是步骤1,这是必需的。
2.(可选)这是第2步,它是可选的,不同于。
我不想在转换过程中丢失这张纸条。
3.这是第三步。
您是否有与para
元素匹配的模板?