Xml XSL使用第一个正则表达式的输出写入第二个正则表达式,并用第二个正则表达式的hit的祖先覆盖子字符串
我有这样的XML:Xml XSL使用第一个正则表达式的输出写入第二个正则表达式,并用第二个正则表达式的hit的祖先覆盖子字符串,xml,xslt,Xml,Xslt,我有这样的XML: <c hw="A"> <e hw="aardvark"> <d t="see <a onclick="goToEntryWithId('2319')">mammals</a> for more details."/> </e> </c> 其中,在XML中: <c hw="M"> <e h
<c hw="A">
<e hw="aardvark">
<d t="see <a onclick="goToEntryWithId('2319')">mammals</a> for more details."/>
</e>
</c>
其中,在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中引用的数字,并在XML文档中的其他地方找到它,它隐藏在如下位置
3在编号中查找搜索结果的祖先::c[@hw]父级
4将onclick表达式替换为简单的href=[filename].html[]
5对XML中出现的每个onclick=goToEntryWithId执行此操作,包括对同一行上的多个点击
有什么想法可以实现吗?您没有说要使用哪个版本的XSLT和哪个XSLT处理器,但假设您可以使用Saxon 9.6,那么您至少可以使用XPath 3.0 parse xml fragment函数将编码的标记解析为节点,然后进一步处理它们,只需要正则表达式来查找和提取onclick属性 基于此,我创建了一个示例输入
<root>
<references>
<c hw="A">
<e hw="aardvark">
<d t="see <a onclick="goToEntryWithId('2319')">mammals</a> 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>
使用Saxon 9.6他或Saxon 9.5 EE see创建输出
<html>
<body>
<p>see <a href="M.html#2319">mammals</a> for more details.
</p>
</body>
</html>
我建议你把你的问题分成几个部分,因为这里有多个问题,彼此无关。使用一个键,通过给定的id查找节点相对来说很简单。寻找它的祖先也是如此。OTOH,处理转义HTML代码既困难又容易出错。[filename]应该来自哪里?