xmlxsltditadita-ot

Convert text string to xml tagging using xsl?


If I have

<p>!!!This should be a note</p>

is it possible to convert the <p> tags to <note> tags using XSL (maybe using the !!! as an identifier?). Note that there would be other <p> tags that I wouldn't want to be converted. So the result would be

<note>!!!This should be a note</note>

or

<note>This should be a note</note>

Also, if I have

<p>This is an apiname</p>

and I want to use XSL to surround 'apiname' with <apiname> tags, example:

<p>This is an <apiname>apiname</apiname></p>

is there a way to do this?

I'm using DITA Open Toolkit to write Markdown, which is then converted to XML behind the scenes. Then, XSL is applied to the XML to display HTML and other formats. This is why I can't just update the XML from the source.


Solution

  • To convert only p elements that start with !!! to note elements, you could do simply:

    XSLT 1.0

    <xsl:stylesheet version="1.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="p[starts-with(., '!!!')]">
        <note>
            <xsl:apply-templates/>
        </note>
    </xsl:template>
    
    </xsl:stylesheet>
    

    If you also want to remove the !!! identifier, change the 2nd template to:

    <xsl:template match="p[starts-with(., '!!!')]">
        <note>
            <xsl:value-of select="substring-after(., '!!!')" />
        </note>
    </xsl:template>
    

    I suggest you ask a separate question regarding the apiname problem, and clarify:

    1. What exactly is apiname? Is it the literal string "apiname"?
    2. Can a p contain more than one occurrence of apiname?
    3. Which version of XSLT does your processor support?