重命名属性和元素 [英] Rename attributes and elements

查看:101
本文介绍了重命名属性和元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有:

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


< pets>

< cats>

< cat1 color =" black" />

< cat2 color =" red" />

< / cats>

< / pets>


我想获得下一个输出:


misus1 color2 =" black"

misus2 color2 =" red"


, - 将元素cat1更改为misus1,将颜色属性更改为color2。

我该怎么办?这个?

if I have:
<?xml version="1.0" encoding="UTF-8"?>

<pets>
<cats>
<cat1 color="black"/>
<cat2 color="red"/>
</cats>
</pets>


And I want to get next output:

misus1 color2="black"
misus2 color2="red"

,- change element cat1 to misus1, attribute color to color2.
How can I do this?

推荐答案

有几种方法可以获得所需的输出。

使用XML DOM可能是合适的,但取决于你的应用程序语言实现可能会有所不同。

但是,我建议使用XSL。

这很简单,需要30分钟。学习。请参阅此链接
There are several ways to get the desired output.
Using XML DOM could be appropriate, but depending on your application language the implementation can differ.
However, I recommend to use XSL.
This is very simple and takes 30 min. to learn. See this link.



有几种方法可以获得所需的输出。

使用XML DOM可能是合适的,但根据您的应用程序语言,实现可能会有所不同。

但是,我建议使用XSL。

这很简单,需要30分钟。学习。请参阅此链接



我试图用for each来实现这个目的。和属性集 (对于每个元素来改变它们的名字......),但我得到一个错误意外的孩子,它指向属性集...


I tried to implement this using "for each" and "attribute-set" (for each element to change their names...), but I get an error "unexpected child", it points to attribute-set...


这就是我写的:


<?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"版本= QUOT; 1.0"缩进= QUOT;是" />


< xsl:attribute-set name =" catsSet">

< xsl:attribute name =" color2"> ; @ color< / xsl:attribute>

< / xsl:attribute-set>


< xsl:template match =" /" >

< xsl:for-each select =" pets / cats / *">

< xsl:element name =" misus1" use-attribute-sets =" catsSet" />

< / xsl:for-each>

< / xsl:template>

< / xsl:stylesheet>


但是我没有得到颜色属性(黑色,红色)的值,它打印的@ color。我怎么能得到它?
Thats what I wrote:

<?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" version="1.0" indent="yes" />

<xsl:attribute-set name="catsSet">
<xsl:attribute name="color2">@color</xsl:attribute>
</xsl:attribute-set>

<xsl:template match="/">
<xsl:for-each select="pets/cats/*">
<xsl:element name="misus1" use-attribute-sets="catsSet"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


But I dont get the value of color attribute (black, red), its printed "@color". How can I get it?


这篇关于重命名属性和元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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