Convert xml with xslt
I have the following xml:
<fo:block font-weight="bold" font-style="italic">Some Text Here</fo:block>
I need to convert it to the following using xsl:
{\b\iSome Text Here\i0\b0\par}
So far I have managed to select a block element using:
<xsl:template match="fo:block">
<xsl:text>{</xsl:text>
<xsl:apply-templates />
<xsl:text>\par}</xsl:text></xsl:template>
and I output: {Some Text Here\par}
I struggle with attributes and insert them using xsl, can someone give me an example of choosing these attributes and getting the desired result?
Here's a fairly simple way to do this in general terms using template modes and instructions <xsl:sort>.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="fo:block">
<xsl:text>{</xsl:text>
<xsl:apply-templates select="@*" mode="prefix" />
<xsl:apply-templates select="node()" />
<xsl:apply-templates select="@*" mode="suffix">
<xsl:sort order="descending"/>
</xsl:apply-templates>
<xsl:text>\par}</xsl:text>
</xsl:template>
<xsl:template match="@font-weight['bold']" mode="prefix">\b</xsl:template>
<xsl:template match="@font-style['italic']" mode="prefix">\i</xsl:template>
<xsl:template match="@font-weight['bold']" mode="suffix">\b0</xsl:template>
<xsl:template match="@font-style['italic']" mode="suffix">\i0</xsl:template>
</xsl:stylesheet>
<xsl:sort order="descending" /> processes the attributes in reverse the second time the suffix mode is used.
Strictly speaking, select="node()"in the middle of the main template is redundant, but it makes it clearer when it reads that only nodes are processed, not attributes.
, suffix :
<xsl:template match="@*" mode="suffix">
<xsl:apply-templates select="." mode="prefix" />
<xsl:text>0</xsl:text>
</xsl:template>
0 . , .
<xsl:template match="fo:block">
<xsl:text>{</xsl:text>
<xsl:if test="@font-weight = 'bold'">\b</xsl:if>
<xsl:if test="@font-style = 'italic'">\i</xsl:if>
<xsl:apply-templates />
<xsl:if test="@font-style = 'italic'">\i0</xsl:if>
<xsl:if test="@font-weight = 'bold'">\b0</xsl:if>
<xsl:text>\par}</xsl:text>
</xsl:template/>
w3schools.com XSLT.
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:variable name="vAttributesResult">
<xsl:call-template name="processAttributes"/>
</xsl:variable>
<xsl:value-of select=
"concat(substring-before($vAttributesResult, ' '),
.,
substring-after($vAttributesResult, ' ')
)
"/>
</xsl:template>
<xsl:template name="processAttributes">
<xsl:param name="pattrList" select="@*"/>
<xsl:param name="pResult" select="' '"/>
<xsl:choose>
<xsl:when test="not($pattrList)">
<xsl:value-of select="$pResult"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vthisResult">
<xsl:apply-templates select="$pattrList[1]">
<xsl:with-param name="pResult" select="$pResult"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:call-template name="processAttributes">
<xsl:with-param name="pattrList" select="$pattrList[position()>1]"/>
<xsl:with-param name="pResult" select="$vthisResult"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@font-weight[.='bold']">
<xsl:param name="pResult"/>
<xsl:value-of select="concat('\b', $pResult, '\b0')"/>
</xsl:template>
<xsl:template match="@font-style[.='italic']">
<xsl:param name="pResult"/>
<xsl:value-of select="concat('\i', $pResult, '\i0')"/>
</xsl:template>
</xsl:stylesheet>
XML- (, ):
<fo:block font-weight="bold" font-style="italic"
xmlns:fo="some:fo">Some Text Here</fo:block>
:
\i\bSome Text Here\b0\i0
:
/ - .
, :
-
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/*">
<xsl:text>{</xsl:text>
<xsl:apply-templates select="@*">
<xsl:with-param name="pSuff" select="''"/>
<xsl:sort select="position()"/>
</xsl:apply-templates>
<xsl:apply-templates select="node()" />
<xsl:apply-templates select="@*">
<xsl:with-param name="pSuff" select="'0'"/>
<xsl:sort select="position()" order="descending"/>
</xsl:apply-templates>
<xsl:text>\par}</xsl:text>
</xsl:template>
<xsl:template match="@font-weight['bold']">
<xsl:param name="pSuff"/>
<xsl:value-of select="concat('\b',$pSuff)"/>
</xsl:template>
<xsl:template match="@font-style['italic']">
<xsl:param name="pSuff"/>
<xsl:value-of select="concat('\i',$pSuff)"/>
</xsl:template>
</xsl:stylesheet>
@Flynn1169 ( 3 t ) , , .
, !
XML-:
<fo:block font-style="italic" font-weight="bold"
xmlns:fo="some:fo">Some Text Here</fo:block>
:
{\i\bSome Text Here\b0\i0\par}
. XPath " ", XSLT, ( 9) push-, .