XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 21st, 2004, 03:27 AM
matrix2k24 matrix2k24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 23 matrix2k24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
xslt

i am having a xml like this

<node att="1">
<element1>
------
</element>
</node>
<node att="2">
<element1>
------
</element>
</node>
<node att="3">
<element1>
------
</element>
</node>

now i wud like to insert a new element <newelement att1="y"/> into this xml based on some condition. e.g. if the node is the last element.

so the expected output is

<node att="1">
<element1>
------
</element>
</node>
<node att="2">
<element1>
------
</element>
</node>
<node att="3">
<element1>
------
<newelement att1="y"/>
</element>
</node>

i know itz possible thro' xslt & i wrote one. But this inserts the new element at the end like this

<node att="1">
<element1>
------
</element>
</node>
<node att="2">
<element1>
------
</element>
</node>
<node att="3">
<element1>
------
</element>
</node>
<newelement att1="y"/>

so my questions are :

1. how to insert as in the expected output ?
2. how to write a test condition to check whether the att="some value" ?

Reply With Quote
  #2  
Old August 21st, 2004, 06:45 AM
jarra jarra is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: the Netherlands
Posts: 66 jarra User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 52 m 39 sec
Reputation Power: 5
Is this what your looking for?

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="name.xslt"?>
<root>
<node att="1">
<element>
hjhjhjh
</element>
</node>
<node att="2">
<element>
jkjkjjk
</element>
</node>
<node att="3">
<element>
jkjkjkjkj
</element>
</node>
<newelement att1="y"/>
</root>


XSL:

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

<xsl:template match="node[position() != last()]">
<node att="{@att}">
<element>
<xsl:value-of select="element"/>
</element>
</node>
</xsl:template>

<xsl:template match="node[position() = last()]">
<node att="{@att}">
<element>
<xsl:value-of select="element"/>
<xsl:apply-templates select="//newelement" mode="node"/>
</element>
</node>
</xsl:template>

<xsl:template match="newelement" mode="node">
<newelement att1="{@att1}"/>
</xsl:template>

<xsl:template match="newelement">
</xsl:template>

</xsl:stylesheet>

result:

<?xml version="1.0" encoding="UTF-8"?>
<node att="1">
<element>
hjhjhjh
</element>
</node>
<node att="2">
<element>
jkjkjjk
</element>
</node>
<node att="3">
<element>
jkjkjkjkj
<newelement att1="y"/></element>
</node>


best,
jarra
__________________
http://www.jarra.nl

Reply With Quote
  #3  
Old August 21st, 2004, 07:14 AM
matrix2k24 matrix2k24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 23 matrix2k24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
a small change:

xml :

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="name.xslt"?>
<mroot>
<mnode att="1">
<melement>
hjhjhjh
</melement>
</mnode>
<mnode att="2">
<melement>
jkjkjjk
</melement>
</mnode>
<mnode att="3">
<melement>
jkjkjkjkj
</melement>
</mnode>
</mroot>


my xsl :


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="add">
<newelement name="nnnnnnn"/>
</xsl:variable>

<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:for-each select="current()">
<xsl:if test="name()='mnode'">
<xsl:if test='position()=last()'>
<xsl:for-each select="melement">
<xsl:copy-of select="$add"/>
</xsl:for-each>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>


real output:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="name.xslt"?>
<mroot>
<mnode att="1">
<melement>
hjhjhjh
</melement>
</mnode>
<mnode att="2">
<melement>
jkjkjjk
</melement>
</mnode>
<mnode att="3">
<melement>
jkjkjkjkj
</melement>
<newelement name="nnnnnnn"/>
</mnode>
</mroot>



expected output:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="name.xslt"?>
<mroot>
<mnode att="1">
<melement>
hjhjhjh
</melement>
</mnode>
<mnode att="2">
<melement>
jkjkjjk
</melement>
</mnode>
<mnode att="3">
<melement>
jkjkjkjkj
<newelement name="nnnnnnn"/>
</melement>
</mnode>
</mroot>


Now can u help me..

Reply With Quote
  #4  
Old August 23rd, 2004, 07:28 AM
matrix2k24 matrix2k24 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 23 matrix2k24 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
done it

here is the solution.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="new">
<newelement name="hii"/>
</xsl:variable>
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//mycriteria">
<xsl:copy>
<xsl:copy-of select="$new"/>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > xslt


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT