xslt - Add text to XML child element using XSL while preserving all children and attributes of children -
i'm trying add text paragraph based on 'performance' attribute of it's 'step' parent.
if step marked 'performance="optional"' resulting text (for second step below) this:
"2. (optional) step 2..."
<procedure> <step id="step_lkq_c1l_5j"> <para>this step 1, required.</para> </step> <step performance="optional"> <para>this step 2, optional, unlike <xref linkend="step_lkq_c1l_5j"/>. <note> <para>i don't want lose note in transformation.</para> </note> </para> </step> <step> <para>this step 3.</para> </step> </procedure>
i tried using xpath match node , modify it: <xsl:template match="step[@performance='optional']/child::para[position()=1]">
, using concat() attempt prepend optional text, lose xref link (possibly because concat() doesn't respect children , attributes?)
i've gotten sort of close want using following xsl customization, (optional) text sits outside paragraph , drops step text down line , breaks page content. want genereated text inside first paragraph.
does have suggestion?
<!-- add "(optional) " steps have performance="optional" attribute set --> <xsl:template match="procedure/step|substeps/step"> <xsl:variable name="id"> <xsl:call-template name="object.id"/> </xsl:variable> <xsl:variable name="keep.together"> <xsl:call-template name="pi.dbfo_keep-together"/> </xsl:variable> <fo:list-item xsl:use-attribute-sets="list.item.spacing"> <xsl:if test="$keep.together != ''"> <xsl:attribute name="keep-together.within-column"><xsl:value-of select="$keep.together"/></xsl:attribute> </xsl:if> <fo:list-item-label end-indent="label-end()"> <fo:block id="{$id}"> <!-- dwc: fix 1 step procedures. use bullet if there's no step 2 --> <xsl:choose> <xsl:when test="count(../step) = 1"> <xsl:text>•</xsl:text> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="." mode="number"> <xsl:with-param name="recursive" select="0"/> </xsl:apply-templates>. </xsl:otherwise> </xsl:choose> </fo:block> </fo:list-item-label> <xsl:choose> <xsl:when test="@performance='optional'"> <fo:list-item-body start-indent="body-start()"> <fo:block> <xsl:text>(optional) </xsl:text> <xsl:apply-templates/> </fo:block> </fo:list-item-body> </xsl:when> <xsl:otherwise> <fo:list-item-body start-indent="body-start()"> <fo:block> <xsl:apply-templates/> </fo:block> </fo:list-item-body> </xsl:otherwise> </xsl:choose> </fo:list-item> </xsl:template>
it's not clear question whether want know how add "(optional)" text, or whether want incorporated xsl-fo, or whether want "2." numbering well. here's simple answer first case:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="step[@performance = 'optional']/para/text()[1]"> <xsl:value-of select="concat('(optional) ', normalize-space())"/> </xsl:template> </xsl:stylesheet>
when run on sample input, produces:
<procedure> <step id="step_lkq_c1l_5j"> <para>this step 1, required.</para> </step> <step performance="optional"> <para>(optional) step 2, optional, unlike<xref linkend="step_lkq_c1l_5j" />. <note><para>i don't want lose note in transformation.</para></note></para> </step> <step> <para>this step 3.</para> </step> </procedure>
to numbering, can replace second template this:
<xsl:template match="step[@performance = 'optional']/para/text()[1]"> <xsl:variable name="sequence"> <xsl:number level="multiple" count="step"/> </xsl:variable> <xsl:value-of select="concat($sequence, '. (optional) ', normalize-space())"/> </xsl:template>
which produces:
<procedure> <step id="step_lkq_c1l_5j"> <para>this step 1, required.</para> </step> <step performance="optional"> <para>2. (optional) step 2, optional, unlike<xref linkend="step_lkq_c1l_5j" />. <note><para>i don't want lose note in transformation.</para></note></para> </step> <step> <para>this step 3.</para> </step> </procedure>
and number steps, can replace same template this:
<xsl:template match="step/para/text()[1]"> <xsl:variable name="sequence"> <xsl:number level="multiple" count="step"/> </xsl:variable> <xsl:value-of select="concat($sequence, '. ')"/> <xsl:if test="../../@performance = 'optional'"> <xsl:text>(optional) </xsl:text> </xsl:if> <xsl:value-of select="normalize-space()"/> </xsl:template>
which produces:
<procedure> <step id="step_lkq_c1l_5j"> <para>1. step 1, required.</para> </step> <step performance="optional"> <para>2. (optional) step 2, optional, unlike<xref linkend="step_lkq_c1l_5j" />. <note><para>i don't want lose note in transformation.</para></note></para> </step> <step> <para>3. step 3.</para> </step> </procedure>
Comments
Post a Comment