transform '?oxy_comments' into xml tag

43 Views Asked by At

Hej!

is there a way to transform ?oxy_comment_start into a valid xml tag (like app></app>) with xslt?

<p>
    <?oxy_custom_start type="oxy_content_highlight" color="0,255,0"?>        
    <?oxy_comment_start author="author" timestamp="today" comment="this: is a comment"?>
    THIS
    <?oxy_custom_end?>
    <?oxy_comment_end mid="102"?>
</p>

what I want

<app>
   <lem>
       THIS
   </lem>
   <rdg> 
      this: is a comment 
   </rdg>
</app>

what I've got:

<xsl:template match="?oxy_comment_start">
    <app>
        <lem>
            <xsl:apply-templates/>
        </lem>
        <rdg>
            <xsl:apply-templates select="@*[local-name() !='comment']"/>            
        </rdg>
    </app>
</xsl:template>

  

I get the error message: "Description: Cannot convert the expression {.?oxy_comment_start} to a pattern"

Does anyone know if this is possible and how?

EDIT: more context of my xml and the '?oxy_comment'. And version is: version="2.0", indentation.

1

There are 1 best solutions below

0
michael.hor257k On

What you have are 4 processing instructions with a text node in-between them.

You can process the 2nd processing instruction using something like:

<xsl:template match="processing-instruction('oxy_comment_start')">
    <app>
        <lem>
            <xsl:value-of select="following-sibling::text()[1]"/>
        </lem>
        <rdg> 
            <xsl:value-of select="." />
        </rdg>
    </app>
</xsl:template>

which should give you:

<app>
   <lem>
    THIS
    </lem>
   <rdg>author="author" timestamp="today" comment="this: is a comment"</rdg>
</app>

How to parse the value of the comment property out of the contents depends on which processor you use. In XSLT 2.0 you could do something like:

<xsl:value-of select="replace(., '.*?comment=&quot;(.*?)&quot;.*', '$1')" />

assuming there won't be any (escaped) quotes inside the values.


Note that you need to be careful not to apply templates indiscriminately from the context of the parent p. Or add an empty template to match its text node. Otherwise it will be processed by the built-in template rule and will appear at the output again, after the app element.