473,568 Members | 3,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT: how to create dynamic elemant name?

12 New Member
Hi All,

I am newbie to XSLT, so I need some help from you guys.
I need to create the XSLT file to transform the element 'name' to 'bname1' and 'bname2' respectively (please see source and required xml below). Could anyone please give me a hint how to do it. Thank you for any help in advance.

Source XML

<books>
<book>
<name>booknam e</name>
<name>authornam e</name>
</book>
</books>

Required XML

<books>
<book>
<bname1>booknam e</bname1>
<bname2>authorn ame</bname2>
</book>
</books>
Jan 11 '08 #1
3 29166
jkmyoung
2,057 Recognized Expert Top Contributor
- Use <xsl:number> to get the right number.
- Store the element name in a variable.
- Declare the element. Use braces {} to get the value of the name attribute from the variable.
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="name">
  2.   <xsl:variable name="ename">
  3.     <xsl:text>bname</xsl:text>
  4.     <xsl:number count="name"/>
  5.   </xsl:variable>
  6.  
  7.   <xsl:element name="{$ename}">
  8.      <xsl:value-of select="."/>
  9.   </xsl:element>
  10. </xsl:template>
  11.  
Jan 11 '08 #2
daydreaming
12 New Member
Thank you jkmyoung. I have applied your idea to what I want to do, but when I have more elements, the xsl:variable value did not not change, so I got the wrong result. Please see below for more information what I've done.

Source XML.

<Delivery>
<ShipTo>
<Name>Jame Good</Name>
<Name>A Com.</Name>
<Address>A Street</Address>
<Address>A City</Address>
</ShipTo>
<BillTo>
<Name>Bill Good</Name>
<Name>B Com.</Name>
<Address>B City</Address>
<Address>B City</Address>
<Address>B State</Address>
</BillTo>
</Delivery>

Existing XSL.

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

<xsl:template match="/Delivery">
<Delivery>
<xsl:apply-templates/>
</Delivery>
</xsl:template>

<xsl:template match="//*/Name">
<xsl:variable name="newname">
<xsl:choose>
<xsl:when test="//ShipTo/Name">
<xsl:text>ShipT oName</xsl:text>
</xsl:when>
<xsl:when test="//BillTo/Name">
<xsl:text>BillT oName</xsl:text>
</xsl:when>
</xsl:choose>
<xsl:number count="Name"/>
</xsl:variable>
<xsl:element name="{$newname }">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

<xsl:template match="//*/Address">
<xsl:variable name="newname">
<xsl:choose>
<xsl:when test="//ShipTo/Address">
<xsl:text>ShipT oAddress</xsl:text>
</xsl:when>
<xsl:when test="//BillTo/Address">
<xsl:text>BillT oAddress</xsl:text>
</xsl:when>
</xsl:choose>
<xsl:number count="Address"/>
</xsl:variable>
<xsl:element name="{$newname }">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

Existing result which is still wrong.

<?xml version="1.0" encoding="UTF-8"?>
<Delivery>

<ShipToName1>Ja me Good</ShipToName1>
<ShipToName2> A Com.</ShipToName2>
<ShipToAddress1 >A Street</ShipToAddress1>
<ShipToAddress2 >A City</ShipToAddress2>

<ShipToName1>Bi ll Good</ShipToName1> * the name did not change to BillToName1
<ShipToName2> B Com.</ShipToName2> * the name did not change to BillToName2
<ShipToAddress1 >B Street</ShipToAddress1> * the name did not change to BillToAddress1
<ShipToAddress2 >B City</ShipToAddress2> * the name did not change to BillToAddress2
<ShipToAddress3 >B State</ShipToAddress3> * the name did not change to BillToAddress3

</Delivery>


The required result XML

<Delivery>
<ShipToName1>Ja me Good</name>
<ShipToName2> A Com.</name>
<ShipToAddress1 >A Street</ShipToAddress1>
<ShipToAddress2 >A City</ShipToAddress2>
<BillToName1>Bi ll Good</BillToName1>
<BillToName2>Bi ll Good</BillToName2>
<BillToAddress1 >B Com.</BillToAddress1>
</Delivery>


Can anyone suggest me how to fix the xsl to get the result as requirement please. Thank you advance.
Jan 13 '08 #3
daydreaming
12 New Member
Finally, I get it done myself with the following code.

<?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" indent="yes" xalan:indent-amount="2" xmlns:xalan="ht tp://xml.apache.org/xslt"/>

<!-- Remove blank line -->
<xsl:template match="text()">
<xsl:value-of select="normali ze-space(.)"/>
</xsl:template>

<xsl:template match="//ShipTo | //BillTo ">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="//ShipTo/*">
<xsl:choose>
<xsl:when test="local-name(.) = 'Name'">
<xsl:call-template name="Name">
<xsl:with-param name="word" select="'ShipTo Name'"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="local-name(.) = 'Address'">
<xsl:call-template name="Address">
<xsl:with-param name="word" select="'ShipTo Address'"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="//BillTo/*">
<xsl:choose>
<xsl:when test="local-name(.) = 'Name'">
<xsl:call-template name="Name">
<xsl:with-param name="word" select="'BillTo Name'"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="local-name(.) = 'Address'">
<xsl:call-template name="Address">
<xsl:with-param name="word" select="'BillTo Address'"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template name="Name">
<xsl:param name="word"/>
<xsl:variable name="newname">
<xsl:value-of select="$word"/>
<xsl:number count="Name"/>
</xsl:variable>
<xsl:element name="{$newname }">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

<xsl:template name="Address">
<xsl:param name="word"/>
<xsl:variable name="newname">
<xsl:value-of select="$word"/>
<xsl:number count="Address"/>
</xsl:variable>
<xsl:element name="{$newname }">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

<!-- Copy other elements -->
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
Jan 14 '08 #4

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

Similar topics

1
3150
by: Luca | last post by:
Hi, have a problem with xslt posted below, this stylesheet format a table like Yahoo directory, but I need exclude (from xslt) all categ element without sublink element (in this case category with name categ2 and categ6 ) and format table in same manner.... I have add a variable named numrows, for dynamic display rows, is possible?
2
3391
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only happening when I put my XML files and the .xsl files on my ISP's system for my home page. If I try to open the XML files in Netscape 7.0 on my own machine...
4
21196
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a native English speaker, it's quite hard to make the problem clear. Please see the following example.
4
4812
by: schneider | last post by:
Anyone know if there is a way to dynamicly create a Xslt template/s and use them as an xml transform with-out use files for the Xslt? All the methods I see use files. I want to create a Xslt transform via code? Any examples would be great.
1
1868
by: Ric Castagna | last post by:
Greetings from Snowy Charlotte... I am trying to create a dynamic web site that will be "skinned" based upon an authenticated user's information. This skinning could be extensive (entire change to the UI appearance) or more minimal with a measure of personalization such as the adding of links to a navigational menu option. I've done...
5
1886
by: Sunil Varma | last post by:
Hi, I've to write a function similar to this. int process(const vector<int>& vct,int key) { // Here I've to find the position of key in the vector and do some processing. }
2
22756
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values in the attribute of an element. Instead of using the <xsl:attribute> element, you can simply place the xpath in the attribute itself. The most...
3
3691
by: saritha2008 | last post by:
Hi, As part of migrating bugs from JIRA to BugZilla, below is the requirement: If there are multiple “version” tags in my input.xml file For open bugs (Resolved,Reopened,Inprogress,NeedInfo) > create multiple bugs, one for each “version". “Fix version” will be empty. For closed bugs > create multiple bugs, one for each “Fixed in” version. ...
2
4354
by: djnokturnal | last post by:
Hey guys / gals, First time posting and of course I am sure it is something that has been answered 100 times but for some reason I just cant find the answer :) First off here is the structure of the xml: <Stats> <Structure> <Column DisplayName="GP" FieldName="GamesPlayed" Priority="1" /> <Column DisplayName="G"...
0
7913
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7659
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6274
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2099
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.