I have an xml that looks something like this
<para>
text text text
<b>text</b> text text <i>text</i>
</para>
the objective is to convert this to mediaWiki formatting with ”’ for a bold font and so on.
when I write a transformation for this the template match ignores all the text inside the <para> tag and only the the <b>s and the <i>s are converted. i need help.
update: here is what i have tried so far:
this is what i have tried so far.
<xsl:template match="para">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="b">
<xsl:text>'''</xsl:text><xsl:value-of select="replace(replace(.,'\s+$',''),'^\s+','')" disable-output-escaping="no"/><xsl:text>'''</xsl:text>
</xsl:template>
<xsl:template match="i">
<xsl:text>''</xsl:text><xsl:value-of select="replace(replace(.,'\s+$',''),'^\s+','')" disable-output-escaping="no"/><xsl:text>''</xsl:text>
</xsl:template>
this is what I used when i tried the text() function.
<xsl:template match="text()">
<xsl:value-of select="." disable-output-escaping="no"/>
</xsl:template>
–update–
in order to not lose the spaces before and after the text block and the bold and italics flags we can also check for spaces before and after the text.
<xsl:template match="text()">
<xsl:variable name="originalText" select="."/>
<xsl:if test="starts-with($originalText,' ')">
<xsl:text> </xsl:text>
</xsl:if>
<xsl:value-of select="normalize-space(.)" disable-output-escaping="no"/>
<xsl:if test="ends-with($originalText,' ')">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
Your problem is exactly in this template. Your codes ignores anything else except element – children. the
*abbreviation stands forchild::element(). Thus the text-nodes children ofparaaren’t processed at all.Solution:
Simply remove the above template. Then the built-in XSLT template for element nodes will be selected to process the
paraelement. All it does is<xsl:apply-templates/>and this is an abbreviation for<xsl:apply-templates select="child::node()"/>. This applies templates for every child node (of any type, including text nodes). When the text-node children ofparaare processed, the built-in XSLT template for text node is selected — all it does is to copy the text as is.The transformation now becomes:
when applied on the provided XML document:
the result now includes the text node children of
para: