2011-09-21 132 views
1

有人能告訴我下面修復的最簡單方法嗎?我目前有一個文件,其中包含多種方法來定義交叉引用(基本上鍊接到其他頁面),並且我想將其中的2個轉換爲單一格式。下面的XML是一個簡化的樣品示出源格式:XSLT字符串操作

<Paras> 
<Para tag="CorrectTag"> 
<local xml:lang="en">Look at this section <XRef XRefType="(page xx)">(page 36)</XRef> for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
<local xml:lang="en">Look at some other section (page <XRef XRefType="xx">52</XRef>) for more information</local> 
</Para> 
</Paras> 

我想要實現的是以下內容:

<Paras> 
<Para tag="CorrectTag"> 
    <local xml:lang="en">Look at this section <XRef XRefType="(page xx)" XRefPage="36"/> for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
    <local xml:lang="en">Look at some other section <XRef XRefType="(page xx)" XRefPage="52"/> for more information</local> 
</Para> 
</Paras> 

使用下面XSLT轉換的[外部參照]元素

<xsl:template match="XRef"> 
    <xsl:copy> 
     <xsl:attribute name="XRefType">(page xx)</xsl:attribute> 
     <xsl:choose> 
      <xsl:when test="@XRefType='(page xx)'"> 
       <xsl:attribute name="XRefPage" select="substring-before(substring-after(.,'(page '),')')"/> 
      </xsl:when> 
      <xsl:when test="@XRefType='xx'"> 
       <xsl:attribute name="XRefPage" select="."/> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:copy> 
</xsl:template> 

已經給我這個輸出:

<Paras> 
<Para tag="CorrectTag"> 
    <local xml:lang="en">Look at this section<XRef XRefType="(page xx)" XRefPage="36"/>for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
    <local xml:lang="en">Look at some other section (page<XRef XRefType="(page xx)" XRefPage="52"/>) for more information</local> 
</Para> 
</Paras> 

哪一個已經解決了我的大部分問題,但我一直在堅持如何在不刪除太多其他內容的情況下清理[local]元素的其餘部分。我需要的是這樣的:如果字符串「(page」後面跟着一個XRef元素,然後將其刪除,如果字符串「)」前面有一個XRef元素,請將其刪除。否則,請勿觸摸它們。

有關如何解決這個問題的任何建議?

謝謝!

回答

1

你應該能夠解決這個問題,例如模板

<xsl:template match="text()[ends-with(., '(page ')][following-sibling::node()[1][self::XRef]]"> 
    <xsl:value-of select="replace(., '(page $', '')"/> 
</xsl:template> 

<xsl:template match="text()[starts-with(., ')')][preceding-sibling::node[1][self::XRef]"> 
    <xsl:value-of select="substring(., 2)"/> 
</xsl:template> 

當然,您需要確保這些文本節點的父元素的任何模板執行apply-templates來處理子節點。

+0

再次感謝馬丁,它的確有竅門。如果有人喜歡重複使用,那麼存在一個小的錯字:[之前的兄弟姐妹::節點[1] [self :: XRef]]需要[之前的兄弟姐妹::節點()[1] [self :: XRef]] 。 – Wokoman