2013-04-09 62 views
0

我試圖將一些文本添加到基於其「步驟」父項的「性能」屬性的段落中。使用XSL將文本添加到XML子元素,同時保留子元素的所有子元素和屬性

如果步驟被標記爲「性能=‘可選的’」我想生成的文本(對於低於第二步驟),以看起來像這樣:

「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()沒有按不尊重孩子和屬性?)

我已經得到了種類的clo使用下面的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> 
+0

你有沒有符合你'para'元素模板? – 2013-04-09 07:19:23

回答

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> 
相關問題