469,361 Members | 2,363 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,361 developers. It's quick & easy.

Rename attributes and elements

8
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?
Mar 2 '07 #1
12 3211
dorinbogdan
839 Expert 512MB
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.
Mar 2 '07 #2
Uliana
8
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...
Mar 3 '07 #3
Uliana
8
Thats what I wrote:

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

<xsl:attribute-set name="catsSet">
<xsl:attribute 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?
Mar 3 '07 #4
Uliana
8
Thank U,
cjanged @color to select value... It works!
Mar 4 '07 #5
dorinbogdan
839 Expert 512MB
Since the problem was solved I'll close the thread.
Mar 21 '07 #6
dorinbogdan
839 Expert 512MB
I re-open the thread in order to allow further comments or suggestions from other members or experts, as per adim request.
Mar 22 '07 #7
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?
Apr 11 '07 #8
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN...

Please post your XSL code, and part of XML.

Thanks,
Dorin.
Apr 12 '07 #9
So, I am trying to do the following (or similar).

Expand|Select|Wrap|Line Numbers
  1.    <xsl:attribute-set name="divs">
  2.       <xsl:attribute name="id"><xsl:value-of select="@id" /></xsl:attribute>
  3.       <xsl:attribute name="style">position:absolute;width:100%;height:100%;overflow:auto;visibility:hidden;</xsl:attribute>
  4.    </xsl:attribute-set>
OR

Expand|Select|Wrap|Line Numbers
  1. <xsl:attribute name="id">@id</xsl:attribute>

with the command:

Expand|Select|Wrap|Line Numbers
  1. <xsl:element name="div" use-attribute-sets="divs"></xsl:element>
based on the XML

Expand|Select|Wrap|Line Numbers
  1. <page id="contact" visibility="show">
  2.             <heading>Contact</heading>
  3.             <content>
  4.                      <paragraph></paragraph>
  5.             </content>
  6.       </page>
Apr 13 '07 #10
dorinbogdan
839 Expert 512MB
Try with {@id} as in:
Expand|Select|Wrap|Line Numbers
  1. <xsl:attribute name="id">{@id}</xsl:attribute>
If not working, please post much XSL code needed to test with.
Apr 13 '07 #11
No, it didn't work either. Here is the XSL file.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4.  
  5.    <xsl:attribute-set name="divs">
  6.       <xsl:attribute name="id">{@id}</xsl:attribute>
  7.       <xsl:attribute name="style">position:absolute;width:100%;height:100%;overflow:auto;visibility:hidden;</xsl:attribute>
  8.    </xsl:attribute-set>
  9.  
  10.    <xsl:template match="/">
  11.       <html>
  12.         <head>
  13.            <title>MJM Enterprises</title>
  14.            <script type="text/javascript">
  15.               function initialise(){
  16.                  alert(document.getElementById("home").style.height);
  17.               }
  18.            </script>
  19.            <style type="text/css">
  20.                    <!-- Omitted -->
  21.            </style>
  22.         </head>
  23.         <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">
  24.            <table cellspacing="0" cellpadding="0" style="width:100%;height:100%;">
  25.               <tr style="background:white;">
  26.                  <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>
  27.                  <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>
  28.                  <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>
  29.               </tr>
  30.               <tr style="background:white;">
  31.                  <td style="vertical-align:bottom;border-bottom:solid orange 3px;">
  32.                     <ul id="nav">
  33.                     <xsl:for-each select="site/page[@visibility='show']">
  34.                        <xsl:choose>
  35.                           <xsl:when test="menu_caption='Home'">
  36.                              <li><a id="home" href=""><xsl:value-of select="menu_caption" /></a></li>
  37.                           </xsl:when>
  38.                           <xsl:when test="menu_caption='Contact'">
  39.                              <li><a id="contact" href=""><xsl:value-of select="menu_caption" /></a></li>
  40.                           </xsl:when>
  41.                           <xsl:otherwise>
  42.                              <li><a href=""><xsl:value-of select="menu_caption" /></a></li>
  43.                           </xsl:otherwise>
  44.                        </xsl:choose>
  45.                     </xsl:for-each>
  46.                     </ul>
  47.                  </td>
  48.               </tr>
  49.               <tr style="height:100%;">
  50.                  <td colspan="3">
  51.                     <table cellpadding="50" cellspacing="0" width="100%" height="100%">
  52.                        <tr>
  53.                           <td style="width:50%;vertical-align:top;">
  54.                              <div id="content_pane" style="height:100%;width:100%;background-color:666666;opacity:0.6;filter: alpha(opacity=60);padding:15px;">
  55.                                 <xsl:for-each select="site/page">
  56.                                    <xsl:element name="div" use-attribute-sets="divs">
  57.                                       <h1 style="font-size:22px;font-style:italic;"><xsl:value-of select="heading" /></h1>
  58.                                       <hr />
  59.                                    </xsl:element>
  60.                                 </xsl:for-each>
  61.                              </div>
  62.                           </td>
  63.                           <td width="50%"><div id="content_right" style="width:100%;height:100%;padding:15px;overflow:auto;"></div></td>
  64.                        </tr>
  65.                     </table>
  66.                  </td>
  67.               </tr>
  68.               <tr>
  69.                   <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>
  70.               </tr>
  71.            </table>
  72.         </body>
  73.       </html>
  74.       </xsl:template>
  75. </xsl:stylesheet>
Thank you
Apr 13 '07 #12
dorinbogdan
839 Expert 512MB
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:attribute-set name="divs">
<xsl:attribute name="style">position........"</xsl:attribute>
</xsl:attribute-set>[/html]
......
and then:[html]<xsl:element name="div" id="{@id}" use-attribute-sets="divs">[/html]
Apr 16 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by John L. Clark | last post: by
9 posts views Thread by Soren Kuula | last post: by
7 posts views Thread by Info 3000 | last post: by
2 posts views Thread by pintihar | last post: by
1 post views Thread by Tod Johnson | last post: by
6 posts views Thread by Jakub.Bednarczuk | last post: by
reply views Thread by David Lozzi | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.