xsltlife
xsltlife

Reputation: 163

How use regex for character after a digit

I want to select @reId which has a character after a digit( fig-FigF.3A ).

Input:

<p type="TOC_Level Two Entry">
  <doclink refType="anchor" refId="fig-FigF.3A">Figure F.3A—Text<c
      type="TOC_Leader Dots"><t/></tps:c></tps:doclink>
  <ref format="TOC Page Number" refType="anchor" refId="fig-FigF.3A"/>
<p>

Output should be:

<p type="TOC_Level Two Entry"><doclink refType="anchor" 
  refId="fig-FigF.3A">F.3A<tps:t/>Text<c 
  type="TOC_Leader Dots"><t/></c></tps:doclink><ref
   format="TOC Page Number" refType="anchor" refId="fig-FigF.3A"/></tps:p>

Tried code:

I tried to solve this with this regex ^(Figure )(\d+|[A-Z].\d+)(—)(.*). But it not workes.

How can I solve this? I am using xslt 2.0

Upvotes: 0

Views: 73

Answers (2)

Sam
Sam

Reputation: 841

Ist is not well-formed your Input plz check

if you want only text change then use this code with replace function:

Input:

    <?xml version="1.0" encoding="UTF-8"?>
<p type="TOC_Level Two Entry">
<tps:doclink refType="anchor" refId="fig-FigF.3A" xmlns:tps="htttp:\\tps">Figure F.3A—Text<tps:c type="TOC_Leader Dots"><t/></tps:c></tps:doclink>
<ref format="TOC Page Number" refType="anchor" refId="fig-FigF.3A"/>
</p>

code:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output method="xml" omit-xml-declaration="no"/>

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

    <xsl:template match="text()">
        <xsl:value-of select="replace(., '(Figure )([A-Z])([.])([0-9A-Z]+)(.+?)([A-Za-z]+)', '$2$3$4')"/>
    </xsl:template>

</xsl:stylesheet>

output:

    <?xml version="1.0" encoding="UTF-8"?>
<p type="TOC_Level Two Entry">
<tps:doclink xmlns:tps="htttp:\\tps" refType="anchor" refId="fig-FigF.3A">F.3B<tps:c type="TOC_Leader Dots"><t/></tps:c></tps:doclink>
<ref format="TOC Page Number" refType="anchor" refId="fig-FigF.3A"/>
</p>

DEMO: https://xsltfiddle.liberty-development.net/ncntCS9/1

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163322

So, trying to extract a clear requirements statement from this, it seems you want the input "fig-FigF.3A" to result in the output "F.3A". Alternatively, perhaps you want to treat "Figure F.3A—Text" as the input? On the one hand you say you are selecting the @reId attribute -- which doesn't exist in your input; on the other hand your attempt at a solution is looking for the text "Figure" which appears in a text node, rather than an attribute.

So I think we need a much clearer requirements statement.

The other problem with this as a requirements statement is that you only really give one example, not a general rule. There's a hint of a general rule in your question "which has a character after a digit". But what does this mean? Your example seems to be looking for the pattern letter-dot-digit, which doesn't match your description of the problem at all.

Sorry, SO moderators, this isn't an answer, it's a comment on the question. It started as an answer, until I realised the question wasn't clear, but by then it was too long for a comment.

Upvotes: 1

Related Questions