I'm attempting to look for statements that refer to other statements using regular expression text matching. It works fine for instances where the text is in the same node, but I am struggling to deal with text that is a child node or split across nodes. Additionally, I want to ignore any text inside a del tag.
Starting with a document like this:
<doc>
<sectionA>
<statement id="1">
<title>Titlle A</title>
<statementtext id="a">This is referring to statement 2 about the stuff</statementtext>
<!-- This is referring to statement <ref statementNumber="2">2</ref> about the stuff -->
</statement>
<statement id="2">
<title>Title B</title>
<statementtext id="b">This is <b>my</b> statement <b>1</b> referring to something else</statementtext>
<!-- This is <b>my</b> statement <ref statementNumber="1"><b>1</b></ref> referring to something else -->
</statement>
<statement id="3">
<title>Title 3</title>
<statementtext id="c">This is another statement <b>1</b><i>2</i> about the stuff</statementtext>
<!-- This is another statement <ref statementNumber="12"><b>1</b><i>2</i></ref> about the stuff -->
</statement>
<statement id="4">
<title>Title 4</title>
<statementtext id="d">This is corrected statement <del>1</del><ins>2</ins> about the stuff</statementtext>
<!-- This is corrected statement <ref statementNumber="2"><del>1</del><ins>2</ins></ref> about the stuff -->
</statement>
<statement id="5">
<title>Title 5</title>
<statementtext id="e">This is partially corrected statement 1<del>1</del><ins>5</ins> about the stuff</statementtext>
<!-- This is partially corrected statement <ref statementNumber="15">1<del>1</del><ins>5</ins></ref> about the stuff -->
</statement>
<statement id="6">
<title>Title 6</title>
<statementtext id="f">This is another
<statementtext id="g"> that contains a nested satementtext for statement <b>1</b><i>3</i> about </statementtext>
the stuff</statementtext>
<!-- This is another <statementtext id="g"> that contains a nested satementtext for statement <ref statementNumber="13"><b>1</b><i>3</i></ref> about </statementtext> -->
</statement>
<statement id="7">
<title>Title 7</title>
<statementtext id="h">This is <i>statement</i> <b>1</b> referring to something else</statementtext>
<!-- This is my <i>statement</i> <ref statementNumber="1"><b>1</b></ref> referring to something else -->
</statement>
<statement id="8">
<title>Title 8</title>
<statementtext id="i">This is has no reference to another statement</statementtext>
<!-- his is has no reference to another statement -->
</statement>
</sectionA>
</doc>
Using my current template
<xsl:template match="statementtext">
<statementtext>
<xsl:copy-of select="./@*" />
<xsl:variable name="thisText">
<xsl:value-of select="./descendant-or-self::text()"/>
</xsl:variable>
<xsl:variable name="thisTextFiltered">
<xsl:value-of select="./descendant-or-self::text()[not(descendant-or-self::del and comment())]"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="matches($thisTextFiltered,'(statements*)(d+)','i')">
<xsl:analyze-string select="$thisTextFiltered"
regex="(statements*)(d+)"
flags="ix">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<xsl:variable name="statementNumber">
<xsl:value-of select="regex-group(2)"></xsl:value-of>
</xsl:variable>
<ref>
<xsl:attribute name="statementNumber">
<xsl:value-of select="$statementNumber" />
</xsl:attribute>
<xsl:value-of select="regex-group(2)"/>
</ref>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</statementtext>
</xsl:template>
<xsl:template match="@*|*|processing-instruction()|comment()">
<xsl:copy>
<xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()" mode="#current"/>
</xsl:copy>
</xsl:template
This is my output:
<!DOCTYPE HTML>
<doc>
<sectionA>
<statement id="1"><title>Titlle A</title><statementtext id="a">This is referring to statement
<ref statementNumber="2">2</ref> about the stuff
</statementtext>
<!-- This is referring to statement <ref statementNumber="2">2</ref> about the stuff -->
</statement>
<statement id="2"><title>Title B</title><statementtext id="b">This is my statement
<ref statementNumber="1">1</ref> referring to something else
</statementtext>
<!-- This is <b>my</b> statement <b><ref statementNumber="1">1</ref></b> referring to something else -->
</statement>
<statement id="3"><title>Title 3</title><statementtext id="c">This is another statement
<ref statementNumber="12">12</ref> about the stuff
</statementtext>
<!-- This is another statement <ref statementNumber="12"><b>1</b><i>2</i></ref> about the stuff -->
</statement>
<statement id="4"><title>Title 4</title><statementtext id="d">This is corrected statement
<ref statementNumber="12">12</ref> about the stuff
</statementtext>
<!-- This is corrected statement <ref statementNumber="2"><del>1</del><ins>2</ins></ref> about the stuff -->
</statement>
<statement id="5"><title>Title 5</title><statementtext id="e">This is partially corrected statement
<ref statementNumber="115">115</ref> about the stuff
</statementtext>
<!-- This is partially corrected statement <ref statementNumber="15">1<del>1</del><ins>5</ins></ref> about the stuff -->
</statement>
<statement id="6"><title>Title 6</title><statementtext id="f">This is another
that contains a nested satementtext for statement
<ref statementNumber="13">13</ref> about
the stuff
</statementtext>
<!-- This is another <statementtext id="g"> that contains a nested satementtext for statement <ref statementNumber="13"><b>1</b><i>3</i></ref> about </statementtext> -->
</statement>
<statement id="7"><title>Title 7</title><statementtext id="h">This is statement
<ref statementNumber="1">1</ref> referring to something else
</statementtext>
<!-- This is my <i>statement</i> <b><ref statementNumber="1">1</ref></b> referring to something else -->
</statement>
<statement id="8"><title>Title 8</title><statementtext id="i">This is has no reference to another statement</statementtext>
<!-- his is has no reference to another statement -->
</statement>
</sectionA>
</doc>
Am I close or do I completely change my approach
question from:
https://stackoverflow.com/questions/65885997/xslt-analyze-string-and-retain-child-nodes 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…