替换所有XML数据 [英] Replace All XML Data

查看:68
本文介绍了替换所有XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于测试目的,我需要一些代码来循环遍历所有属性

和XML文件中的元素,并用元素替换数据或

属性名称?有没有人会有这样做的代码,通过每个节点循环

? VB.Net代码将不胜感激,所以我可以

看看它是如何完成的。


Derek

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

推荐答案




Derek Hart写道:


Derek Hart wrote:

出于测试目的,我需要一些代码循环遍历所有属性

和XML文件中的元素,并用元素或

属性名称替换数据?有没有人会有这样做的代码,通过每个节点循环

? VB.Net代码将不胜感激,所以我可以

看看它是如何完成的。
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.



XSLT样式表可以做到这一点。但是你的要求不明确,

你想用包含其他元素的元素做什么,例如

< root>

< ;孩子>

< descendant> Kibology< / descendant>

< / child>

< / root>

你想要的root和子元素会发生什么,例如

< root> root

< child> child

< descendant> descendant< / descendant>

< / child>

< / root>

或者你只想要输出元素的元素名称没有

任何子元素,例如例如获得

< root>

< child>

< descendant> descendant< / descendant>

< / child>

< / root>

-


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


我希望这样:


< root> root

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

< / child>

< / root>


你能帮忙解决那些可以做到这一点的代码吗?


Derek


" Martin Honnen" < ma ******* @ yahoo.dewrote in message

news:u6 ************** @ TK2MSFTNGP03.phx.gbl ...
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写道:
>

Derek Hart wrote:

>出于测试目的,我需要一些代码来循环遍历XML文件中的所有属性和元素,并用
元素或属性名称替换数据?有没有人会有这样做的代码,
通过每个节点循环? VB.Net代码将不胜感激
所以我可以看到它是如何完成的。
>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.



XSLT样式表可以做到这一点。但是你的要求不明确,

你想用包含其他元素的元素做什么,例如

< root>

< ;孩子>

< descendant> Kibology< / descendant>

< / child>

< / root>

你想要的root和子元素会发生什么,例如

< root> root

< child> child

< descendant> descendant< / descendant>

< / child>

< / root>

或者你只想要输出没有任何

子元素的元素的元素名称,例如例如获得

< root>

< child>

< descendant> descendant< / descendant>

< / child>

< / root>


-


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






Derek Hart写道:


Derek Hart wrote:

我会这样想:


< root> root

< child> child

< descendant> descendant< ; / descendant>

< / child>

< / root>


你能帮忙解决一下这个问题。一般这样做吗?
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?



这个样式表应该这样做:


<?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:模板匹配=" @ *">

< 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()和

follow-sibling :: node ()]">

< xsl:value-of select =" concat(name(..),.)" />

< / xsl:template>


< xsl:template match =" * / node()[1] [self: :text()和

not(following-sibling :: node())]"

< xsl:value-of select =" name( ..)" />

< / xsl:template>


< / xsl:stylesheet>


VB.NET 1.x代码用以上运行转换

stylesheet.xsl,file.xml是输入文件名,result.xml是

输出文件名是例如

Dim XsltProcessor As New XslTransform

XsltProcessor.Load(" stylesheet.xsl")

XsltProcessor.Transform(_

新XPathDocument(" file.xml",XmlSpace.Preserve),_

" result.xml",_

什么都没有)


使用VB.NET 2.0你可以做到这一点


Dim XsltProcessor As New XslCompiledTransform()

XsltProcessor.Load(" stylesheet.xsl")

XsltProcessor.Trans form(file.xml,result.xml)


这有帮助吗?

-


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


这篇关于替换所有XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆