使用XSL将文本添加到XML子元素,同时保留子项的所有子项和属性

时间:2013-04-09 07:05:45

标签: xml xslt docbook

我正在尝试根据它的'step'父级的'performance'属性为段落添加一些文本。

如果某个步骤被标记为'performance =“optional”'我希望生成的文本(下面的第二步)看起来像这样:

“2。(可选)这是第2步......”

<procedure>
    <step id="step_lkq_c1l_5j">
        <para>This is step 1, which is required.</para>
    </step>
    <step performance="optional">
        <para>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>

我尝试使用Xpath来匹配我的节点并修改它:<xsl:template match="step[@performance='optional']/child::para[position()=1]">然后使用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>&#x2022;</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>

1 个答案:

答案 0 :(得分:1)

从您的问题中不清楚您是否只想知道如何添加“(可选)”文本,或者您是否希望将其合并到您的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>

要获得编号,您可以用以下内容替换第二个模板:

  <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>

产生:

<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>

要对所有步骤进行编号,您可以使用以下步骤替换相同的模板:

  <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>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>
相关问题