如何将元素分组到一行? 我希望我的所有节点都在一行中,并在名字的孩子之后排序。 我不知道我该怎么做!
> XML输入:
`<parent>
<Node ID="a">
<Name>first</Name>
<ID>0x1</ID>
<info>the first nod</info>
<numb>8</numb>
<Comment></Comment>
<child>
<Name>ch1</Name>
<ID>0x11</ID>
<info>the first child</info>
<numb>7</numb>
<Comment></Comment>
</child>
...
<other>
<Name>attr</Name>
<Value>No</Value>
<DefaultUsed>No</DefaultUsed>
</other>
<other>
<Name>attr</Name>
<Value>No</Value>
<DefaultUsed>No</DefaultUsed>
</other>
</Node>
<Node ID="b">
<Name>second</Name>
<ID>0x2</ID>
<info>the second nod</info>
<numb>9</numb>
<Comment></Comment>
<child>
<Name>ch2</Name>
<ID>0x22</ID>
<info>the second child</info>
<numb>7</numb>
<Comment></Comment>
</child>
....
<other>
<Name>attr1</Name>
<Value>No</Value>
<DefaultUsed>No</DefaultUsed>
</other>
<other>
<Name>attr1</Name>
<Value>No</Value>
<DefaultUsed>No</DefaultUsed>
</other>
</Node>
</parent>
`
XML输出
`<Node ; "Name= first","ID=0x1","info=the first nod","numb=8">
<child ;"Name= ch1","ID=0x11","info=the first child","numb=7">
....
<Node ; "Name= second","ID=0x2","info=the second nod","numb=9">
<child ;"Name= ch2","ID=0x22","info=the second child","numb=7">
`
有人能帮助我吗? 非常感谢
答案 0 :(得分:0)
您显示的输出不是格式良好的XML。您可以获得输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Node Name="first" ID="0x1" info="the first nod" numb="8">
<child Name="ch1" ID="0x11" info="the first child" numb="7"/>
</Node>
<Node Name="second" ID="0x2" info="the second nod" numb="9">
<child Name="ch2" ID="0x22" info="the second child" numb="7"/>
</Node>
</root>
将以下样式表应用于您的输入:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/parent">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="Node | child">
<xsl:copy>
<xsl:apply-templates select="*[not(*)]"/>
<xsl:apply-templates select="child"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[text()]">
<xsl:attribute name="{name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
要按child
对Name
元素进行排序,请更改:
<xsl:apply-templates select="child"/>
为:
<xsl:apply-templates select="child">
<xsl:sort select="Name" data-type="text" order="ascending"/>
</xsl:apply-templates>