问题描述
我有一个复杂的xml文件,希望转换成非常简单的xml格式。
I have a complex xml file, want to convert into very simpler xml format.
任何人都可以请求我怎么转换?
Can anybody pleas suggest how can i convert?
复杂文件:
Complex File :
<?xml version="1.0" encoding="utf-8" ?>
< ElementData>
<ElementData>
< Element ElementName =" Element1" ID = QUOT 1 QUOT;类别= QUOT; CAT1">
< Column name =" Tag1"> ValueOfTag1< / Column>
< Column name =" Tag2"> ValueOfTag2< / Column>
< Column name =" Tag3"> ValueOfTag3< / Column>
< Column name =" Tag4"> ValueOfTag4< / Column>
< / Element>
< Element ElementName =" Element2" ID = QUOT; 2英寸类别= QUOT; CAT2">
< Column name =" Tag1"> ValueOfTag1< / Column>
< Column name =" Tag2"> ValueOfTag2< / Column>
< Column name =" Tag3"> ValueOfTag3< / Column>
< Column name =" Tag4"> ValueOfTag4< / Column>
< / Element>
---
---
---
< / ElementData>
<Element ElementName="Element1" Id="1" Category="Cat1"> <Column name="Tag1">ValueOfTag1</Column> <Column name="Tag2">ValueOfTag2</Column> <Column name="Tag3">ValueOfTag3</Column> <Column name="Tag4">ValueOfTag4</Column> </Element> <Element ElementName="Element2" Id="2" Category="Cat2"> <Column name="Tag1">ValueOfTag1</Column> <Column name="Tag2">ValueOfTag2</Column> <Column name="Tag3">ValueOfTag3</Column> <Column name="Tag4">ValueOfTag4</Column> </Element> --- --- ---</ElementData>
我想将其转换为以下格式:
I Want to convert it to below format:
<?xml version="1.0" encoding="utf-8" ?>
< elementData中>
< Element>
< ElementName> Element1< / ElementName>
< Id> 1< / Id>
< Category> Cat1< / Category>
< Tag1> ValueOfTag1< / Tag1>
< Tag2> ValueOfTag2< / Tag2>
< Tag3> ValueOfTag3< / Tag3>
< Tag4> ValueOfTag4< / Tag4>
< / Element>
< Element>
< ElementName> Element2< / ElementName>
< Id> 2< / Id>
< Category> Cat2< / Category>
< Tag1> ValueOfTag1< / Tag1>
< Tag2> ValueOfTag2< / Tag2>
< Tag3> ValueOfTag3< / Tag3>
< Tag4> ValueOfTag4< / Tag4>
< / Element>
---
---
---
< / ElementData>
<ElementData> <Element> <ElementName>Element1</ElementName> <Id>1</Id> <Category>Cat1</Category> <Tag1>ValueOfTag1</Tag1> <Tag2>ValueOfTag2</Tag2> <Tag3>ValueOfTag3</Tag3> <Tag4>ValueOfTag4</Tag4> </Element> <Element> <ElementName>Element2</ElementName> <Id>2</Id> <Category>Cat2</Category> <Tag1>ValueOfTag1</Tag1> <Tag2>ValueOfTag2</Tag2> <Tag3>ValueOfTag3</Tag3> <Tag4>ValueOfTag4</Tag4> </Element> --- --- ---</ElementData>
问候,
推荐答案
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:element name="{local-name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="Column[@name]">
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
在Visual Studio中,您可以将转换应用为XML编辑功能的一部分,当然,您可以在代码中运行转换
Within Visual Studio you can apply transformation as part of the XML editing features and of course you can run transformations in codeusing System.Xml.Xsl.XslCompiledTransform
这篇关于从复杂的XML文件转换更简单的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!