结束标记前的 XSLT 目标特定字符串

XSLT target specific string before closing tags

我目前正在尝试向元素添加新属性,但该值需要来自数据本身,而且我不知道如何定位它,因为文本值可能出现在两个不同的地方。

我的输入XML如下:

案例一

<div>
   <title/>
   <p>This is an example where the string is being used in the text (0123-45-6789) and how a sentence can look like. (0123-45-6789)</p>
</div>

案例二

<div>
   <title>This is an example title. (0123-45)</title>
   <p>This is an example sentence.</p>
</div>

目标

<div id="0123-45">
    <title>This is an example title. (0123-45)</title>
    <p>This is an example sentence.</p>
</div>

我需要的字符串是括号之间的字符串,它可以由2位、4位、6位或10位组成。由于该字符串也可以在文本中使用,因此我只能针对结束标记

和 .

之前的字符串

我已经尝试将 analyze-stringregex 一起使用,但最终以所有字符串为目标,而不是我需要的字符串。

有什么方法可以在 XSLT 中完成吗?在此先感谢您指出正确的方向!

亲切的问候

怎么样:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="div">
    <div id="{replace(., '.*\((.*)\).*', '')}">
        <xsl:apply-templates/>
    </div>
</xsl:template>

</xsl:stylesheet>