Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add attribute value to all elements?

Status
Not open for further replies.

mariod1014

Technical User
Jul 1, 2004
3
US
I have an XML file that requires that the attribute os=" WINDOWS" be included in all elements. Right now some elements include that attribute, but not all. What's the best/easiest way to insert that attribute in all elements where its missing? Thanks for any help.

Mario
 
You could do it easily with XSL:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:if test="not(@os)">
        <xsl:attribute name="os">WINDOWS</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
But why are you doing this? Can you not use a namespace?

Jon
 
I'm not a programmer by trade, but a technical writer and I had two weeks to deliver an XML document based on a DTD from the customer. So I only had time to learn XML and have not gotten the time to learn other aspects, as for example, XSL. However, this is all very intriguing and I hope to come to speed on that too. Thanks very much for your help.
Mario
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top