How to use xsl to update an xml file

I have this xsl snippet, which I want to apply to an xml file. The xpath data in match is correct, I have tested it. I want to add a font attribute to every ITEXT that does not have one with a specified value. How would I do this?

    <xsl:template match="//PAGEOBJECT[@LAYER=2]/ITEXT[not(@FONT)]">
        <xsl:attribute name="FONT">Book Antiqua Regular</xsl:attribute>
    </xsl:template>

Sample XML snippet:

<ITEXT FONT="Book Antiqua Italic" KERN="-1" CH="somedata"/>
<ITEXT CH="somedata"CH="somedata"/>
<ITEXT FONT="Book Antiqua Italic" CH="somedata"/>
<ITEXT CH="somedata"/>
<ITEXT FONT="Book Antiqua Italic" CH="somedata"/>
<ITEXT FONT="Book Antiqua Italic" CH="somedata"/>
<ITEXT FONT="Book Antiqua Italic" CH="somedata"/>
<ITEXT CH="somedata"/>
<ITEXT CH="somedata"/>

Answers


You're most of the way there. Start with the Identity Stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Insert additional templates here.... -->

</xsl:stylesheet>

Where marked, insert the template to handle your special case -- which was almost right, except that you need to copy the element that you're adding the attribute to, and you need to copy the other attributes and children (it's "identity but add this attribute")

<xsl:template match="//PAGEOBJECT[@LAYER=2]/ITEXT[not(@FONT)]">
   <xsl:copy>
     <xsl:attribute name="FONT">Book Antiqua Regular</xsl:attribute>
     <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

Note that you may not need that complex a match expression; it's probably sufficient to say match="ITEXT[not(@FONT)]"


Need Your Help

How to get from specific Model to ViewModel and vice versa, using nested classes?

c# asp.net-mvc entity-framework model viewmodel

I want to know a good way of converting the Model to ViewModel and ViewModel to Model without AutoMapper or something similar, because I want to understand what is behind and learn how to do it mys...

WSDL: No element type is defined for message

web-services wsdl axis2 bpel apache-ode

I'm creating a service orchestration using Eclipse BPEL Designer plugin and i have a problem with the WSDL file that it generates automatically.