我正在使用以下XML
<Root>
<sample>1</sample>
<sample>2</sample>
<sample>3</sample>
<sample>4</sample>
<sample>5</sample>
<sample>6</sample>
</Root>
我想得到如下所示的输出
<sample>123456</sample>
我正在使用下面的XSLT来获得上面的输出。但是我得到了这样的输出。
<sample>1</sample>
<sample>23456</sample>
<sample>2</sample>
<sample>3456</sample>
<sample>3</sample>
<sample>456</sample>
<sample>4</sample>
<sample>56</sample>
<sample>5</sample>
<sample>6</sample
><sample>6</sample>
<sample></sample>
这是我尝试过的XSL代码:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="sample">
<presample>
<xsl:value-of select="."/>
<xsl:variable name="code" select="following-sibling::sample" />
<xsl:for-each select="following-sibling::sample">
<xsl:if test="not(preceding-sibling::sample)">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</presample>
</xsl:template>
</xsl:stylesheet>
请帮我纠正这个XSLT以获得上面提到的所需输出。
答案 0 :(得分:2)
这是怎么回事:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="sample[1]">
<sample>
<xsl:for-each select=". | following-sibling::sample">
<xsl:value-of select="."/>
</xsl:for-each>
</sample>
</xsl:template>
<xsl:template match="sample[position() > 1]" />
</xsl:stylesheet>
我认为你的最终目标比这更具参与性,所以如果你能详细说明,可能会有更好的一般方法。
不确定这是否适合你,但这里有一个更通用的方法:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*[text()[normalize-space(.)] and not(name(preceding-sibling::*[1]) = name())]">
<xsl:copy>
<xsl:variable name="list" select=". | following-sibling::*"/>
<xsl:for-each select="$list">
<xsl:variable name="pos" select="position()" />
<xsl:if test="not($list[position() < $pos and name() != name(current())])">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="*[name(preceding-sibling::*[1]) = name()]" />
<xsl:template match="text()" />
</xsl:stylesheet>
运行时:
<Root>
<sample>1</sample>
<sample>2</sample>
<sample>3</sample>
<sample>4</sample>
<sample>5</sample>
<sample>6</sample>
<child>
<something>4</something>
<something>5</something>
<something>6</something>
<something>7</something>
<somethingelse>a</somethingelse>
<somethingelse>b</somethingelse>
<somethingelse>c</somethingelse>
<somethingelse>d</somethingelse>
<something>8</something>
<something>9</something>
<something>10</something>
</child>
</Root>
产生这个:
<sample>123456</sample>
<something>4567</something>
<somethingelse>abcd</somethingelse>
<something>8910</something>
答案 1 :(得分:0)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="sample[not(preceding-sibling::sample)]">
<sample>
<xsl:for-each select=". | following-sibling::sample">
<xsl:value-of select="."/>
</xsl:for-each>
</sample>
</xsl:template>
<xsl:template match="sample[preceding-sibling::sample]" />
</xsl:stylesheet>
输入XML :
<?xml version="1.0" encoding="utf-8"?>
<root>
<sample>1</sample>
<sample>2</sample>
<sample>3</sample>
<sample>4</sample>
<dummy1>a</dummy1>
<dummy2>b</dummy2>
<dummy3>c</dummy3>
<dummy4>d</dummy4>
<sample>5</sample>
<sample>6</sample>
<sample>7</sample>
<sample>8</sample>
</root>
输出将是:
<?xml version="1.0" encoding="utf-8"?>
<root>
<sample>12345678</sample>
<dummy1>a</dummy1>
<dummy2>b</dummy2>
<dummy3>c</dummy3>
<dummy4>d</dummy4>
</root>
答案 2 :(得分:0)
这是Muenchian分组方法的简单应用 - 比使用兄弟轴更有效的分组方法:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kSampleByVal" match="sample" use="."/>
<xsl:template match="sample[1]" priority="2">
<sample>
<xsl:apply-templates select=
"../*[generate-id()=generate-id(key('kSampleByVal',.)[1])]/text()"/>
</sample>
</xsl:template>
<xsl:template match="*/*"/>
</xsl:stylesheet>
将此转换应用于以下XML文档(提供的修改后更具代表性):
<Root>
<sample>1</sample>
<sample>2</sample>
<sample>3</sample>
<sample>4</sample>
<sample>5</sample>
<sample>6</sample>
<sample>3</sample>
<sample>4</sample>
<sample>1</sample>
<sample>2</sample>
<sample>3</sample>
</Root>
产生了想要的正确结果:
<sample>123456</sample>
请注意:
我建议在所有情况下使用Muenchian分组方法而不是兄弟姐妹比较分组。 Muenchian具有O(N)时间复杂度,而兄弟姐妹 - 比较分组的时间复杂度是二次方 - O(N ^ 2)。我已经看到兄弟 - 比较分组需要40分钟,而Muenchian分组只用了2秒钟。