Reputation: 589
Input:
<para>
<text>1. this a paragaraph1</text>
<text>12. this a paragaraph2</text>
<text>2. this a paragaraph2</text>
<text>this a paragaraph3</text>
</para>
My current output:
<result>
<para type="number">1. this a paragaraph1</para>
<para type="number">12. this a paragaraph2</para>
<para type="number">2. this a paragaraph2</para>
<para type="not number">this a paragaraph3</para>
</result>
Output should be:
<result>
<para type="number">1. this a paragaraph1</para>
<para type="number double">12. this a paragaraph2</para>
<para type="number">2. this a paragaraph2</para>
<para type="not number">this a paragaraph3</para>
</result>
Explanation of the logic:
You can see para/text
has a string. Some of them are starting with numbers (1.
, 12.
) then .
and space. At that time para/@type
must be number
. If the count of that number is 1
@type should be number
. If the count of that number is 2
@type should be number double
. You can see the example.
Tried code:
<xsl:template match="text">
<xsl:choose>
<xsl:when test="matches(.,'^[0-9]+\. ')">
<para type="number">
<xsl:value-of select="."/>
</para>
</xsl:when>
<xsl:otherwise>
<para type="not number">
<xsl:value-of select="."/>
</para>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="para">
<xsl:apply-templates/>
</xsl:template>
I am using XSLT 2.0
.
Upvotes: 3
Views: 320
Reputation: 116959
How about:
<xsl:template match="text">
<xsl:copy>
<xsl:attribute name="type">
<xsl:choose>
<xsl:when test="matches(., '^\d{2}\. ')">double</xsl:when>
<xsl:when test="matches(., '^\d\. ')">number</xsl:when>
<xsl:otherwise>not number</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
Upvotes: 3