473,383 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

XSLT: how to create dynamic elemant name?

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>bookname</name>
<name>authorname</name>
</book>
</books>

Required XML

<books>
<book>
<bname1>bookname</bname1>
<bname2>authorname</bname2>
</book>
</books>
Jan 11 '08 #1
3 29094
jkmyoung
2,057 Expert 2GB
- 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
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:stylesheet 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>ShipToName</xsl:text>
</xsl:when>
<xsl:when test="//BillTo/Name">
<xsl:text>BillToName</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>ShipToAddress</xsl:text>
</xsl:when>
<xsl:when test="//BillTo/Address">
<xsl:text>BillToAddress</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>Jame Good</ShipToName1>
<ShipToName2>A Com.</ShipToName2>
<ShipToAddress1>A Street</ShipToAddress1>
<ShipToAddress2>A City</ShipToAddress2>

<ShipToName1>Bill 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>Jame Good</name>
<ShipToName2>A Com.</name>
<ShipToAddress1>A Street</ShipToAddress1>
<ShipToAddress2>A City</ShipToAddress2>
<BillToName1>Bill Good</BillToName1>
<BillToName2>Bill 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
Finally, I get it done myself with the following code.

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

<!-- Remove blank line -->
<xsl:template match="text()">
<xsl:value-of select="normalize-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="'ShipToName'"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="local-name(.) = 'Address'">
<xsl:call-template name="Address">
<xsl:with-param name="word" select="'ShipToAddress'"/>
</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="'BillToName'"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="local-name(.) = 'Address'">
<xsl:call-template name="Address">
<xsl:with-param name="word" select="'BillToAddress'"/>
</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
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...
2
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...
4
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...
4
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...
1
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...
5
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
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...
3
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) >...
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.