xml - XSLT: Create new tags around other tags -


can use xslt transform following xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <root> <p>the chemical formula water h<sub>2</sub>o. rest of paragraph not relevant.</p> <p>another paragraph without subscripts.</p> </root> 

to this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <root> <p><text>the chemical formula water h</text><sub>2</sub><text>o. rest of paragraph not relevant.</text></p> <p>another paragraph without subscripts.</p> </root> 

i.e. wrap different parts of p element contains sub elements text elements?

this xslt far:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:template match="root|p">     <xsl:copy>         <xsl:copy-of select="@*"/>         <xsl:apply-templates/>     </xsl:copy> </xsl:template> <xsl:template match="p[child::*[name()='sub']]">     <xsl:copy>         <xsl:element name="text">             <xsl:copy-of select="@*"/>             <xsl:apply-templates/>         </xsl:element>     </xsl:copy> </xsl:template> <xsl:template match="sub">     <xsl:copy>         <xsl:apply-templates/>     </xsl:copy> </xsl:template> </xsl:stylesheet> 

but wraps whole paragraph <text> element , doesn't divide @ 2. how can make want?


a little background information, if you're interested: want import xml adobe indesign, if apply character style sub sub elements, second half of paragraph (starting @ o) still in subscript. have wrap other bits in <text> in order formatting correct.

match on text nodes:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">  <xsl:template match="@* | node()">   <xsl:copy>     <xsl:apply-templates select="@* | node()"/>   </xsl:copy> </xsl:template>   <xsl:template match="text()[following-sibling::node()[1][self::sub]]                      | text()[preceding-sibling::node()[1][self::sub]]">   <text>     <xsl:value-of select="."/>   </text> </xsl:template>  </xsl:stylesheet> 

of course if don't want behaviour text nodes edit match pattern e.g. match="p/text()[following-sibling::node()[1][self::sub]] | p/text()[preceding-sibling::node()[1][self::sub]]".


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -