2015-04-17 92 views
0

我有這樣的XML:XSL首先使用正則表達式的輸出寫入第二個正則表達式並覆蓋的第二個正則表達式的祖先串打

<c hw="A"> 
    <e hw="aardvark"> 
    <d t="see &lt;a onclick=&quot;goToEntryWithId('2319')&quot;&gt;mammals&lt;/a&gt; for more details."/> 
    </e> 
</c> 

有了,elswhere在XML:

<c hw="M"> 
    <e hw="mammals" i="2319"> 
    <d t="Here's useful info about mammals."/> 
    </e> 
</c> 

我根據標籤將XML分解爲多個HTML文件,並且我想將onclick更改爲具有適當目標的href。

希望的輸出:

<p>See <a href="M.html#2319">mammals</a> for more details. 

我需要以某種

1)找到匹配爲含####一個onclick表達,

2)取數(### #參考),其內部的onclick並在它的隱藏如<e i="####">

3某處在XML文檔中別的地方找到)找到祖先:: C [@hw]搜索的父結果在數

4)與取代的onclick表達簡單href="[filename].html#[####]"

5)執行此操作的XML中的每個onclick="goToEntryWithId('####')發生,包括在同一行上的多個命中。

任何想法,我可能會做到這一點?

+1

我建議你把你的問題分成幾個,因爲你在這裏有多個問題,與每個問題無關其他。使用**鍵**來查找給定ID的節點相對來說不重要。所以找到它的祖先。 OTOH處理逃逸的HTML代碼很困難且容易出錯。 [文件名]應該來自哪裏? –

回答

0

您還沒有說過要使用哪個版本的XSLT和哪個XSLT處理器,但假設您可以使用Saxon 9.6 HE,至少可以使用XPath 3.0 parse-xml-fragment函數將該編碼標記解析爲節點,然後進一步處理它們,只需要正則表達式來查找和提取onclick屬性。

因此,基於我創建一個樣本輸入端

<root> 
    <references> 
    <c hw="A"> 
     <e hw="aardvark"> 
     <d t="see &lt;a onclick=&quot;goToEntryWithId('2319')&quot;&gt;mammals&lt;/a&gt; for more details."/> 
     </e> 
    </c> 
    </references> 
    <content> 
    <c hw="M"> 
     <e hw="mammals" i="2319"> 
     <d t="Here's useful info about mammals."/> 
     </e> 
    </c> 
    </content> 
</root> 

和樣式表

<xsl:stylesheet 
    version="3.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs"> 

<xsl:param name="pattern" as="xs:string">^goToEntryWithId\('[0-9]+'\)$</xsl:param> 

<xsl:output method="html" indent="yes"/> 

<xsl:key name="id" match="content/c/e" use="@i"/> 

<xsl:variable name="main-doc" select="/"/> 

<xsl:template match="/"> 
    <html> 
    <body> 
     <xsl:apply-templates select="root/references//@t"/> 
    </body> 
    </html> 
</xsl:template> 

<xsl:template match="references/c/e/d/@t"> 
    <p> 
    <xsl:apply-templates select="parse-xml-fragment(.)/node()"/> 
    </p> 
</xsl:template> 

<xsl:template match="a[@onclick[matches(., $pattern)]]"> 
    <xsl:variable name="id" select="replace(@onclick, '[^0-9]+', '')"/> 
    <xsl:variable name="referenced-c" select="key('id', $id, $main-doc)/ancestor::c[@hw]"/> 
    <a href="{$referenced-c/@hw}.html#{$id}"> 
    <xsl:apply-templates/> 
    </a> 
</xsl:template> 

</xsl:stylesheet> 

其中使用撒克遜9.6 HE或撒克遜9.5 EE(見http://xsltransform.net/94hvTzw)創建輸出

<html> 
    <body> 
     <p>see <a href="M.html#2319">mammals</a> for more details. 
     </p> 
    </body> 
</html> 
相關問題