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

Replace All XML Data

For testing purposes, I need some code that will loop through all attributes
and elements in an XML file, and replace the data with the element or
attribute name? Does anybody have code that will do this, looping
generically through every node? VB.Net code would be appreciated so I can
see how it is done.

Derek
Aug 19 '06 #1
20 2103


Derek Hart wrote:
For testing purposes, I need some code that will loop through all attributes
and elements in an XML file, and replace the data with the element or
attribute name? Does anybody have code that will do this, looping
generically through every node? VB.Net code would be appreciated so I can
see how it is done.
An XSLT stylesheet could do that. However your requirement is not clear,
what do you want to do with an element that contains other elements e.g.
<root>
<child>
<descendant>Kibology</descendant>
</child>
</root>
what should happen with the root and the child element do you want e.g.
<root>root
<child>child
<descendant>descendant</descendant>
</child>
</root>
or do you only want to output the element name of elements not having
any child elements e.g. for the example get
<root>
<child>
<descendant>descendant</descendant>
</child>
</root>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 19 '06 #2
I would want it this way:

<root>root
<child>child
<descendant>descendant</descendant>
</child>
</root>

Can you help with the code that could do this generically?

Derek

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:u6**************@TK2MSFTNGP03.phx.gbl...
>

Derek Hart wrote:
>For testing purposes, I need some code that will loop through all
attributes and elements in an XML file, and replace the data with the
element or attribute name? Does anybody have code that will do this,
looping generically through every node? VB.Net code would be appreciated
so I can see how it is done.

An XSLT stylesheet could do that. However your requirement is not clear,
what do you want to do with an element that contains other elements e.g.
<root>
<child>
<descendant>Kibology</descendant>
</child>
</root>
what should happen with the root and the child element do you want e.g.
<root>root
<child>child
<descendant>descendant</descendant>
</child>
</root>
or do you only want to output the element name of elements not having any
child elements e.g. for the example get
<root>
<child>
<descendant>descendant</descendant>
</child>
</root>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 19 '06 #3


Derek Hart wrote:
I would want it this way:

<root>root
<child>child
<descendant>descendant</descendant>
</child>
</root>

Can you help with the code that could do this generically?
This stylesheet should do:

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

<xsl:output method="xml" indent="yes"/>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{name()}"
namespace="{namespace-uri()}"><xsl:value-of
select="name()"/></xsl:attribute>
</xsl:template>

<xsl:template match="comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>

<xsl:template match="text()">
<xsl:copy/>
</xsl:template>

<xsl:template match="*/node()[1][self::text() and
following-sibling::node()]">
<xsl:value-of select="concat(name(..), .)" />
</xsl:template>

<xsl:template match="*/node()[1][self::text() and
not(following-sibling::node())]">
<xsl:value-of select="name(..)"/>
</xsl:template>

</xsl:stylesheet>

VB.NET 1.x code to run the transformation with the above being
stylesheet.xsl, file.xml being the input file name and result.xml being
the output file name is e.g.
Dim XsltProcessor As New XslTransform
XsltProcessor.Load("stylesheet.xsl")

XsltProcessor.Transform(_
New XPathDocument("file.xml", XmlSpace.Preserve),_
"result.xml",_
Nothing)

With VB.NET 2.0 you could do e.g.

Dim XsltProcessor As New XslCompiledTransform()
XsltProcessor.Load("stylesheet.xsl")

XsltProcessor.Transform("file.xml", "result.xml")

Does that help?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 19 '06 #4
What imports statements do I use?

I get an error on the Transform line (1.x version) that says: "Overload
resolution failed because no accessible 'Transform' can be called with these
arguments."

Thank You,
Derek
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>

Derek Hart wrote:
>I would want it this way:

<root>root
<child>child
<descendant>descendant</descendant>
</child>
</root>

Can you help with the code that could do this generically?

This stylesheet should do:

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

<xsl:output method="xml" indent="yes"/>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{name()}"
namespace="{namespace-uri()}"><xsl:value-of
select="name()"/></xsl:attribute>
</xsl:template>

<xsl:template match="comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>

<xsl:template match="text()">
<xsl:copy/>
</xsl:template>

<xsl:template match="*/node()[1][self::text() and
following-sibling::node()]">
<xsl:value-of select="concat(name(..), .)" />
</xsl:template>

<xsl:template match="*/node()[1][self::text() and
not(following-sibling::node())]">
<xsl:value-of select="name(..)"/>
</xsl:template>

</xsl:stylesheet>

VB.NET 1.x code to run the transformation with the above being
stylesheet.xsl, file.xml being the input file name and result.xml being
the output file name is e.g.
Dim XsltProcessor As New XslTransform
XsltProcessor.Load("stylesheet.xsl")

XsltProcessor.Transform(_
New XPathDocument("file.xml", XmlSpace.Preserve),_
"result.xml",_
Nothing)

With VB.NET 2.0 you could do e.g.

Dim XsltProcessor As New XslCompiledTransform()
XsltProcessor.Load("stylesheet.xsl")

XsltProcessor.Transform("file.xml", "result.xml")

Does that help?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 19 '06 #5


Derek Hart wrote:
What imports statements do I use?
System.Xml.Xsl
System.Xml.XPath
I get an error on the Transform line (1.x version) that says: "Overload
resolution failed because no accessible 'Transform' can be called with these
arguments."
> Dim XsltProcessor As New XslTransform
XsltProcessor.Load("stylesheet.xsl")

XsltProcessor.Transform(_
New XPathDocument("file.xml", XmlSpace.Preserve),_
"result.xml",_
Nothing)
Sorry, my bad, in my test case I was simply writing the output to
Console.Out and then tried to change the Transform call in the newsgroup
post to create a file but it seems the argument combination I posted is
not possible. One possible way to create a file as the transformation
result is e.g.
XsltProcessor.Transform(_
New XPathDocument(Args(0), XmlSpace.Preserve),_
Nothing,_
New FileStream("result.xml", FileMode.Create), Nothing)

FileStream is in System.IO so you need to import that as well then.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 20 '06 #6
Derek Hart wrote:
I would want it this way:

<root>root
<child>child
<descendant>descendant</descendant>
</child>
</root>
Mixed content like this is unlikely to be useful to you unless
you are accustomed to handling it.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Aug 20 '06 #7
A couple things. And thank you this is really close. I need to close the
filestream, but a variable was not used, so I am not sure how to close it.

And it's a strange thing. About 6 elements did not get filled with data.
About 200 did. But not 6 of them. And they have no particular strange
pattern or anything. Any thoughts?

Derek

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uj**************@TK2MSFTNGP02.phx.gbl...
>

Derek Hart wrote:
>What imports statements do I use?

System.Xml.Xsl
System.Xml.XPath
>I get an error on the Transform line (1.x version) that says: "Overload
resolution failed because no accessible 'Transform' can be called with
these arguments."

>> Dim XsltProcessor As New XslTransform
XsltProcessor.Load("stylesheet.xsl")

XsltProcessor.Transform(_
New XPathDocument("file.xml", XmlSpace.Preserve),_
"result.xml",_
Nothing)

Sorry, my bad, in my test case I was simply writing the output to
Console.Out and then tried to change the Transform call in the newsgroup
post to create a file but it seems the argument combination I posted is
not possible. One possible way to create a file as the transformation
result is e.g.
XsltProcessor.Transform(_
New XPathDocument(Args(0), XmlSpace.Preserve),_
Nothing,_
New FileStream("result.xml", FileMode.Create), Nothing)

FileStream is in System.IO so you need to import that as well then.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 20 '06 #8


Derek Hart wrote:

OK, I have the file closing, but it randomly does not fill in data into
elements. I have attached a file. Any ideas?
Can you post the input XML as well?
I see an empty <Statement></Statementin the result but I need to know
what the original contents of that element was to find out where the
stylesheet fails.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 20 '06 #9


Martin Honnen wrote:

I see an empty <Statement></Statementin the result but I need to know
what the original contents of that element was to find out where the
stylesheet fails.
Thinking about I guess the Statement is empty in the original document,
if you add the template

<xsl:template mach="*[not(node())]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="name()"/>
</xsl:copy>
</xsl:template>

to the stylesheet then it will hopefully fix that.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 20 '06 #10
Wow, that works. One more question: how can I add info to the stylesheet to
specifically prevent an element or attribute from being changed. Then I can
keep the few items that are necessary in the xml intact.

Derek

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uB**************@TK2MSFTNGP04.phx.gbl...
>

Martin Honnen wrote:

>I see an empty <Statement></Statementin the result but I need to know
what the original contents of that element was to find out where the
stylesheet fails.

Thinking about I guess the Statement is empty in the original document, if
you add the template

<xsl:template mach="*[not(node())]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="name()"/>
</xsl:copy>
</xsl:template>

to the stylesheet then it will hopefully fix that.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 20 '06 #11


Derek Hart wrote:
One more question: how can I add info to the stylesheet to
specifically prevent an element or attribute from being changed. Then I can
keep the few items that are necessary in the xml intact.
For those attributes you want to keep you could simple add a template e.g.

<xsl:template match="attributeName1 | attributeName2 | attributeName3">
<xsl:copy/>
</xsl:template>

Adapting the stylesheet to not change certain elements is more difficult
in general and how to do that might depend on what exactly you want to
do with elements and child elements, one way you could try is e.g.

<xsl:template match="elementName1 | elementName2 | elementName3">
<xsl:copy-of select="."/>
</xsl:template>

For those elements listed in the match attribute it will simply copy
them (and all their attributes and child nodes) to the result tree. That
however means the other approach the stylesheet so far does (e.g. the
naming insertion you want) is _not_ at all applied to any of the
attributes and child nodes of the element.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 21 '06 #12
Where you have attributeName1, elementName1, etc..., if I know the exact
xpath to the location, can I use that?

Derek
"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uV**************@TK2MSFTNGP04.phx.gbl...
>

Derek Hart wrote:
>One more question: how can I add info to the stylesheet to specifically
prevent an element or attribute from being changed. Then I can keep the
few items that are necessary in the xml intact.

For those attributes you want to keep you could simple add a template e.g.

<xsl:template match="attributeName1 | attributeName2 | attributeName3">
<xsl:copy/>
</xsl:template>

Adapting the stylesheet to not change certain elements is more difficult
in general and how to do that might depend on what exactly you want to do
with elements and child elements, one way you could try is e.g.

<xsl:template match="elementName1 | elementName2 | elementName3">
<xsl:copy-of select="."/>
</xsl:template>

For those elements listed in the match attribute it will simply copy them
(and all their attributes and child nodes) to the result tree. That
however means the other approach the stylesheet so far does (e.g. the
naming insertion you want) is _not_ at all applied to any of the
attributes and child nodes of the element.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 21 '06 #13


Derek Hart wrote:
Where you have attributeName1, elementName1, etc..., if I know the exact
xpath to the location, can I use that?
It is a match pattern which allows only a subset of all possible XPath
expressions.
The difference is specified here:
<http://www.w3.org/TR/xslt#patterns>

Or simply try the XPath expressions you have for those match patterns,
if the XSLT processor does not complain then use them.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 21 '06 #14
Not sure how to get this to work. What I have is below. The bottom section
has the part where I am trying to not change an attribute of iGenVisible.

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

<xsl:output method="xml" indent="yes"/>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{name()}"
namespace="{namespace-uri()}"><xsl:value-of
select="name()"/></xsl:attribute>
</xsl:template>

<xsl:template match="comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>

<xsl:template match="text()">
<xsl:copy/>
</xsl:template>

<xsl:template match="*/node()[1][self::text() and
following-sibling::node()]">
<xsl:value-of select="concat(name(..), .)" />
</xsl:template>

<xsl:template match="*/node()[1][self::text() and
not(following-sibling::node())]">
<xsl:value-of select="name(..)"/>
</xsl:template>

<xsl:template match="*[not(node())]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="name()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="iGenVisible">
<xsl:copy/>
</xsl:template>

</xsl:stylesheet>

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>

Derek Hart wrote:
>Where you have attributeName1, elementName1, etc..., if I know the exact
xpath to the location, can I use that?

It is a match pattern which allows only a subset of all possible XPath
expressions.
The difference is specified here:
<http://www.w3.org/TR/xslt#patterns>

Or simply try the XPath expressions you have for those match patterns, if
the XSLT processor does not complain then use them.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 22 '06 #15


Derek Hart wrote:
Not sure how to get this to work. What I have is below. The bottom section
has the part where I am trying to not change an attribute of iGenVisible.
<xsl:template match="iGenVisible">
You need
<xsl:template match="@iGenVisible">
to match an attribute.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 22 '06 #16
OK, here is what I have in the stylesheet. Note the bottom item that tries
to leave the LOB element alone. That does not work. What do you think? The
attribute does work.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{name()}"
namespace="{namespace-uri()}"><xsl:value-of
select="name()"/></xsl:attribute>
</xsl:template>

<xsl:template match="comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>

<xsl:template match="text()">
<xsl:copy/>
</xsl:template>

<xsl:template match="*/node()[1][self::text() and
following-sibling::node()]">
<xsl:value-of select="concat(name(..), .)" />
</xsl:template>

<xsl:template match="*/node()[1][self::text() and
not(following-sibling::node())]">
<xsl:value-of select="name(..)"/>
</xsl:template>

<xsl:template match="*[not(node())]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="name()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="Data/@iGenVisible">
<xsl:copy/>
</xsl:template>

<xsl:template match="Data/LOB">
<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>


"Martin Honnen" <ma*******@yahoo.dewrote in message
news:uv*************@TK2MSFTNGP04.phx.gbl...
>

Derek Hart wrote:
>Not sure how to get this to work. What I have is below. The bottom
section has the part where I am trying to not change an attribute of
iGenVisible.
><xsl:template match="iGenVisible">

You need
<xsl:template match="@iGenVisible">
to match an attribute.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 22 '06 #17
Use CSV files, its easier and makes more sence.
Peter Flynn wrote:
Derek Hart wrote:
I would want it this way:

<root>root
<child>child
<descendant>descendant</descendant>
</child>
</root>

Mixed content like this is unlikely to be useful to you unless
you are accustomed to handling it.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Aug 23 '06 #18


Derek Hart wrote:
OK, here is what I have in the stylesheet. Note the bottom item that tries
to leave the LOB element alone. That does not work. What do you think? The
attribute does work.
<xsl:template match="Data/LOB">
<xsl:copy-of select="."/>
</xsl:template>
Can you provide a minimal but complete XML input where that stylesheet
fails?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Aug 23 '06 #19
Martin,

I got it working. I had a non well-formed XML. Sorry about that. It did
not come from me. Thanks for the help. I will email on this thread if
anything else does not work.

Thank You!
Derek Hart

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>

Derek Hart wrote:
>OK, here is what I have in the stylesheet. Note the bottom item that
tries to leave the LOB element alone. That does not work. What do you
think? The attribute does work.
><xsl:template match="Data/LOB">
<xsl:copy-of select="."/>
</xsl:template>

Can you provide a minimal but complete XML input where that stylesheet
fails?
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Aug 23 '06 #20


..

*** Sent via Developersdex http://www.developersdex.com ***
Sep 22 '06 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: hokieghal99 | last post by:
import os, string print " " setpath = raw_input("Enter the path: ") def find_replace(setpath): for root, dirs, files in os.walk(setpath): fname = files for fname in files: find =...
19
by: rbt | last post by:
Here's the scenario: You have many hundred gigabytes of data... possible even a terabyte or two. Within this data, you have private, sensitive information (US social security numbers) about your...
1
by: Luke Dalessandro | last post by:
I have an application where there is a primary XML data file. I'll use the following as an example: <data> <item id="a"> <name>A</name> <price>$10</price> </item> <item id="b">...
2
by: JHB | last post by:
Hi, How can I do a location.replace when I use a form, like when I use a href? This works. <a href="Ny HTML-side20.htm"; method="post" id="frm" name="BrugerHovedSide"...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
2
by: jason | last post by:
Hello. I just converted some data to to sql2000 into a TEXT type field. I needed to remove all tabs from the data and now want to remove extra lines too. As we know, SQL2000's replace function...
4
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to...
4
by: Roy | last post by:
Hey all, Created an .aspx page using VB as the code behind. Compiler pops up error: "BC30451: Name 'Replace' is not declared." Essentially, it acts as if Replace is a custom function that needs...
7
by: .... | last post by:
Hi I have an existing function which has a stream object (inmsg.BodyPart.Data). I'm trying to search and replace the stream object in the most efficient way possible This is my attempt below,...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.