Rename attributes and elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Uliana
    New Member
    • Mar 2007
    • 8

    Rename attributes and elements

    if I have:
    <?xml version="1.0" encoding="UTF-8"?>

    <pets>
    <cats>
    <cat1 color="black"/>
    <cat2 color="red"/>
    </cats>
    </pets>


    And I want to get next output:

    misus1 color2="black"
    misus2 color2="red"

    ,- change element cat1 to misus1, attribute color to color2.
    How can I do this?
  • dorinbogdan
    Recognized Expert Contributor
    • Feb 2007
    • 839

    #2
    There are several ways to get the desired output.
    Using XML DOM could be appropriate, but depending on your application language the implementation can differ.
    However, I recommend to use XSL.
    This is very simple and takes 30 min. to learn. See this link.

    Comment

    • Uliana
      New Member
      • Mar 2007
      • 8

      #3
      Originally posted by dorinbogdan
      There are several ways to get the desired output.
      Using XML DOM could be appropriate, but depending on your application language the implementation can differ.
      However, I recommend to use XSL.
      This is very simple and takes 30 min. to learn. See this link.

      I tried to implement this using "for each" and "attribute-set" (for each element to change their names...), but I get an error "unexpected child", it points to attribute-set...

      Comment

      • Uliana
        New Member
        • Mar 2007
        • 8

        #4
        Thats what I wrote:

        <?xml version="1.0" encoding="UTF-8"?>
        <xsl:styleshe et version="1.0"
        xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" version="1.0" indent="yes" />

        <xsl:attribut e-set name="catsSet">
        <xsl:attribut e name="color2">@ color</xsl:attribute>
        </xsl:attribute-set>

        <xsl:template match="/">
        <xsl:for-each select="pets/cats/*">
        <xsl:element name="misus1" use-attribute-sets="catsSet"/>
        </xsl:for-each>
        </xsl:template>
        </xsl:stylesheet>


        But I dont get the value of color attribute (black, red), its printed "@color". How can I get it?

        Comment

        • Uliana
          New Member
          • Mar 2007
          • 8

          #5
          Thank U,
          cjanged @color to select value... It works!

          Comment

          • dorinbogdan
            Recognized Expert Contributor
            • Feb 2007
            • 839

            #6
            Since the problem was solved I'll close the thread.

            Comment

            • dorinbogdan
              Recognized Expert Contributor
              • Feb 2007
              • 839

              #7
              I re-open the thread in order to allow further comments or suggestions from other members or experts, as per adim request.

              Comment

              • mjmj1059
                New Member
                • Apr 2007
                • 3

                #8
                Originally posted by Uliana
                Thank U,
                cjanged @color to select value... It works!

                can you elaborate? I am trying to create new DIVs from XML and need to set the id attribute of the DIVs from an id attribute of the XML elemtens.

                I have managed to set the style attributes of DIVs using attribute-set (simple string) but cannot load in the @id part. I think this is what you were trying to do.

                Can you help me?

                Comment

                • dorinbogdan
                  Recognized Expert Contributor
                  • Feb 2007
                  • 839

                  #9
                  Welcome to TheScripts TSDN...

                  Please post your XSL code, and part of XML.

                  Thanks,
                  Dorin.

                  Comment

                  • mjmj1059
                    New Member
                    • Apr 2007
                    • 3

                    #10
                    So, I am trying to do the following (or similar).

                    Code:
                       <xsl:attribute-set name="divs">
                          <xsl:attribute name="id"><xsl:value-of select="@id" /></xsl:attribute>
                          <xsl:attribute name="style">position:absolute;width:100%;height:100%;overflow:auto;visibility:hidden;</xsl:attribute>
                       </xsl:attribute-set>
                    OR

                    Code:
                    <xsl:attribute name="id">@id</xsl:attribute>

                    with the command:

                    Code:
                    <xsl:element name="div" use-attribute-sets="divs"></xsl:element>
                    based on the XML

                    Code:
                    <page id="contact" visibility="show">
                                <heading>Contact</heading>
                                <content>
                                         <paragraph></paragraph>
                                </content>
                          </page>

                    Comment

                    • dorinbogdan
                      Recognized Expert Contributor
                      • Feb 2007
                      • 839

                      #11
                      Try with {@id} as in:
                      Code:
                      <xsl:attribute name="id">{@id}</xsl:attribute>
                      If not working, please post much XSL code needed to test with.

                      Comment

                      • mjmj1059
                        New Member
                        • Apr 2007
                        • 3

                        #12
                        No, it didn't work either. Here is the XSL file.

                        Code:
                        <?xml version="1.0" encoding="ISO-8859-1"?>
                        
                        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                        
                           <xsl:attribute-set name="divs">
                              <xsl:attribute name="id">{@id}</xsl:attribute>
                              <xsl:attribute name="style">position:absolute;width:100%;height:100%;overflow:auto;visibility:hidden;</xsl:attribute>
                           </xsl:attribute-set>
                        
                           <xsl:template match="/">
                              <html>
                                <head>
                                   <title>MJM Enterprises</title>
                                   <script type="text/javascript">
                                      function initialise(){
                                         alert(document.getElementById("home").style.height);
                                      }
                                   </script>
                                   <style type="text/css">
                                           <!-- Omitted -->
                                   </style>
                                </head>
                                <body onLoad="initialise()" style="background:white url(i/bg.jpg) no-repeat fixed bottom right;font-family:Arial,Calibri,Courier New;border:0px;margin:0px;padding:0px;border:solid orange 1px">
                                   <table cellspacing="0" cellpadding="0" style="width:100%;height:100%;">
                                      <tr style="background:white;">
                                         <td rowspan="2" style="vertical-align:bottom;border-bottom:solid orange 3px;"><p style="font-size:28;text-indent:30px;padding-top:10px;padding-bottom:0px;margin-bottom:0px;color:666666;font-weight:lighter;">MJM E nterprises</p><p style=",padding-top:0px;margin-top:0px;color:black;font-style:italic;font-size:11px;text-indent:50px;">Photovoltaic Engineers ● Environmentalists &#9679</p></td>
                                         <td align="right"><p style="color:666666;font-size:12px;font-style:italic;">"The solar energy reaching the earth's surface each day exceeds mankind's total energy requirements for thirty years."</p></td>
                                         <td rowspan="2" align="center" style="vertical-align:center;border-bottom:solid orange 3px;"><a href="http://www.conergy.com/"><img src="i/conergy.jpg" alt="Conergy" border="0" height="37px" /></a>   <a href="http://www.greens.org.au/"><img src="i/thegreens.jpg" alt="The Greens" border="0" height="37px" /></a></td>
                                      </tr>
                                      <tr style="background:white;">
                                         <td style="vertical-align:bottom;border-bottom:solid orange 3px;">
                                            <ul id="nav">
                                            <xsl:for-each select="site/page[@visibility='show']">
                                               <xsl:choose>
                                                  <xsl:when test="menu_caption='Home'">
                                                     <li><a id="home" href=""><xsl:value-of select="menu_caption" /></a></li>
                                                  </xsl:when>
                                                  <xsl:when test="menu_caption='Contact'">
                                                     <li><a id="contact" href=""><xsl:value-of select="menu_caption" /></a></li>
                                                  </xsl:when>
                                                  <xsl:otherwise>
                                                     <li><a href=""><xsl:value-of select="menu_caption" /></a></li>
                                                  </xsl:otherwise>
                                               </xsl:choose>
                                            </xsl:for-each>
                                            </ul>
                                         </td>
                                      </tr>
                                      <tr style="height:100%;">
                                         <td colspan="3">
                                            <table cellpadding="50" cellspacing="0" width="100%" height="100%">
                                               <tr>
                                                  <td style="width:50%;vertical-align:top;">
                                                     <div id="content_pane" style="height:100%;width:100%;background-color:666666;opacity:0.6;filter: alpha(opacity=60);padding:15px;">
                                                        <xsl:for-each select="site/page">
                                                           <xsl:element name="div" use-attribute-sets="divs">
                                                              <h1 style="font-size:22px;font-style:italic;"><xsl:value-of select="heading" /></h1>
                                                              <hr />
                                                           </xsl:element>
                                                        </xsl:for-each>
                                                     </div>
                                                  </td>
                                                  <td width="50%"><div id="content_right" style="width:100%;height:100%;padding:15px;overflow:auto;"></div></td>
                                               </tr>
                                            </table>
                                         </td>
                                      </tr>
                                      <tr>
                                          <td colspan="3" style="background:white;border-top:solid orange 1px;border-bottom:solid orange 3px;"><p align="center" style="font-size:11px;color:666666;font-style:italic;">Copyright 2007, MJM Enterprises.<br />Last updated April 10, 2007</p></td>
                                      </tr>
                                   </table>
                                </body>
                              </html>
                              </xsl:template>
                        </xsl:stylesheet>
                        Thank you

                        Comment

                        • dorinbogdan
                          Recognized Expert Contributor
                          • Feb 2007
                          • 839

                          #13
                          I could not test due to some xml errors, maybe not had enough XML data.

                          I would try to define the id attribute right within the xsl:element, for instance
                          [html]<xsl:attribut e-set name="divs">
                          <xsl:attribut e name="style">po sition........" </xsl:attribute>
                          </xsl:attribute-set>[/html]
                          ......
                          and then:[html]<xsl:element name="div" id="{@id}" use-attribute-sets="divs">[/html]

                          Comment

                          Working...