2015-04-17 95 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> 
相关问题