473,322 Members | 1,736 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,322 software developers and data experts.

how to include attribute in XML tag

I'm transforming one xml document to another using a stylesheet.
1.) I would like to know how to include an attribute with a value in a tag, eg. how do I transform
<tag1>hey</tag1> to
<tag1 value1 ="1" value2="text">hey</tag1>

2.)the root tag of the xml document i'm transforming has an attribute.how do I instruct the XSL to retrieve data from a child of this tag using <xsl:template> element and/or <xsl:apply-templates>?

Any ideas would be much appreciated. Thanks y'all!
Jul 20 '07 #1
5 2112
jkmyoung
2,057 Expert 2GB
2.)the root tag of the xml document i'm transforming has an attribute.how do I instruct the XSL to retrieve data from a child of this tag using <xsl:template> element and/or <xsl:apply-templates>?

Any ideas would be much appreciated. Thanks y'all!
1.)
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="tag1">
  2. <xsl:copy>
  3.   <xsl:attribute name="value1">1</xsl:attribute>
  4.   <xsl:attribute name="value2">text</xsl:attribute>
  5.   <xsl:apply-templates/>
  6. </xsl:copy>
  7. </xsl:template>
2. Not sure what you mean, but guessing ?
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/*">
  2.   <xsl:value-of select="someChild"/>
  3. </xsl:template>
Jul 20 '07 #2
1.)
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="tag1">
  2. <xsl:copy>
  3.   <xsl:attribute name="value1">1</xsl:attribute>
  4.   <xsl:attribute name="value2">text</xsl:attribute>
  5.   <xsl:apply-templates/>
  6. </xsl:copy>
  7. </xsl:template>
2. Not sure what you mean, but guessing ?
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="/*">
  2.   <xsl:value-of select="someChild"/>
  3. </xsl:template>

Well thanks for the reply. I tried it and it didn't work. I think I wasn't too clear on my question, so I'll explain a little more.

I'm transforming one xml document into another, the root element in the old doc looks something like
<root xmlns = "http://www.blah/blah">
<element1>
<subelem>hello</subelem>
<subelem2>there</subelem2>
</element1>
</root>

transformation needs to look like:

<new root xmlns:xsi="http://www.ovaloffice" xmlns:fm="http://www.rest">
<newElem1>
<NewSub1>hello</NewSub>
<NewSub2>there</NewSub2>
</newElem1>
</new root>
Jul 23 '07 #3
jkmyoung
2,057 Expert 2GB
the xmlns: attribute is actually a namespace.
Declare a namespace in your stylesheet tag, eg add: xmlns:nms="http://www.blah/blah"
Everywhere you reference the node, use the namespace, eg as examples,
<xsl:template match="nms:root">,
<xsl:value-of select="nms:subelem"/>, and
<xsl:apply-templates select="nms:element1"/>

One sample template:
<xsl:template match"nms:element1">
<newElem1>
<xsl:apply-templates/>
</newElem1>
</xsl:template>
Jul 23 '07 #4
the xmlns: attribute is actually a namespace.
Declare a namespace in your stylesheet tag, eg add: xmlns:nms="http://www.blah/blah"
Everywhere you reference the node, use the namespace, eg as examples,
<xsl:template match="nms:root">,
<xsl:value-of select="nms:subelem"/>, and
<xsl:apply-templates select="nms:element1"/>

One sample template:
<xsl:template match"nms:element1">
<newElem1>
<xsl:apply-templates/>
</newElem1>
</xsl:template>
jkmyoung, I really appreciate your help.I tried your suggestion,but I still cant get what I want. I dont know what I'm doing wrong.here's the stylesheet and it's transformation

<?xml version="1.0"?>
<xsl:stylesheet version ="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.blah/blah">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match ="/">
<new_root xmlns:lm="http://iou.cm">
<xsl:apply-templates/></new_root>
</xsl:template>
<xsl:template match ="m:element1">
<newElem1><xsl:value-of select="m:NewSub1"/></newElem1>
</xsl:template>
</xsl:stylesheet>

here's the output:

<?xml version="1.0" encoding="UTF-16" ?>
<new_root xmlns:m="http://www.blah/blah xmlns:lm="http://iou.cm">
<newElem1>hello</newElem1>
</new_root>


How can I remove xmlns:m ="http://www.blah/blah namespace ?
I dont want it to appear in there
Jul 25 '07 #5
jkmyoung
2,057 Expert 2GB
In the stylesheet element use attribute
exclude-result-prefixes="m"
Jul 26 '07 #6

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

Similar topics

3
by: Zhou Lei | last post by:
Hello now I'm writing the XSL to transform the XML to an html file, and some xml elements are: <user login="loginname"> <name>James Cook</name> </user> I need to include the login attribute...
21
by: Peter Bassett | last post by:
I have a .shtml file, that displays photos, in which I wish to pass some functionality off to an Include file for reusability purposes. Unfortunately, it's not working. Here is a portion of the...
5
by: Viken Karaguesian | last post by:
Hello all, I've used the SSI Include command successfully in the past to include frequently used files that are based within my own site. However, what if I was to use a file located in a...
9
by: skijor | last post by:
I am converting a raw html webiste to use javascript. A lot of the html code is duplicated in many pages. Some of that code can be consolidated into javacript but I don't want to add the same...
3
by: Simon Brooke | last post by:
I'm trying to do internationalisation by using xsl:include to include a different file depending on the locale setting, and I'm completely failing. I've tried several different approaches: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.