Search code examples
xmlxsltwordml

styling based on element attribute?


I'm transforming xml into wordml using xslt. I would like to be able to format content of table cell differently if attribute of element that carries content of that cell is different. For example, I have following xslt:

  <xsl:template match="/ns0:RootElement/ns0:Items/ns0:Item0">
    <w:tc>
      <w:tcPr>
        <w:tcW w:w="2268" w:type="dxa" />
        <w:noWrap />
      </w:tcPr>
      <ns0:Item0>
        <xsl:for-each select="@ns0:*|@*[namespace-uri()='']">
          <xsl:attribute name="{name()}" namespace="{namespace-uri()}">
            <xsl:value-of select="." />
          </xsl:attribute>
        </xsl:for-each>
        <w:p wsp:rsidR="00F75372" wsp:rsidRPr="0058287E" wsp:rsidRDefault="00F75372" wsp:rsidP="0058287E">
          <w:r wsp:rsidRPr="0058287E"> <w:t><xsl:value-of select="." /></w:t></w:r>
        </w:p>
      </ns0:Item0>
    </w:tc>
  </xsl:template>

Let's say that Item0 has attribute selected, I would like to change formating based on this attribute. Any idea about how to modify presented xslt to achieve that? Regards


Solution

  • Here is a solution that worked for me:

    <xsl:template match="/ns0:RootElement/ns0:Items/ns0:Item0">
        <w:tc>
          <w:tcPr>
            <w:tcW w:w="2268" w:type="dxa" />
            <w:noWrap />
              <!-- test if item0 attribute is selected and if it is, change border and background color-->
             <xsl:if test='@selected=1'>
                <w:tcBorders>
                   <w:top w:val="single" w:sz="8" wx:bdrwidth="20" w:space="0" w:color="993300" />
                   <w:left w:val="single" w:sz="8" wx:bdrwidth="20" w:space="0" w:color="993300" />
                   <w:bottom w:val="single" w:sz="8" wx:bdrwidth="20" w:space="0" w:color="993300" />
                   <w:right w:val="single" w:sz="8" wx:bdrwidth="20" w:space="0" w:color="993300" />
                </w:tcBorders>
                <w:shd w:val="clear" w:color="auto" w:fill="FF9900" wx:bgcolor="DD5800" />
             </xsl:if>
          </w:tcPr>
          <ns0:Item0>
            <xsl:for-each select="@ns0:*|@*[namespace-uri()='']">
              <xsl:attribute name="{name()}" namespace="{namespace-uri()}">
                <xsl:value-of select="." />
              </xsl:attribute>
            </xsl:for-each>
            <w:p wsp:rsidR="00F75372" wsp:rsidRPr="0058287E" wsp:rsidRDefault="00F75372" wsp:rsidP="0058287E">
                <!-- test if item0 attribute is selected and if it is, change font to bold-->
               <xsl:if test='@selected=1'>
                  <w:r>
                     <w:rPr>
                        <!--<w:i w:val="on"/>-->
                        <w:b/>
                     </w:rPr>
                     <w:t>
                        <xsl:value-of select="." />
                     </w:t>
                  </w:r>
               </xsl:if>
               <xsl:if test='@selected=-1'>
                  <w:r wsp:rsidRPr="0058287E">
                     <w:t>
                        <xsl:value-of select="." />
                     </w:t>
                  </w:r>
               </xsl:if>
            </w:p>
          </ns0:Item0>
        </w:tc>
      </xsl:template>
    

    Hope somebody might have use of this ...