1

以下大问题:

<root>
<div>
    <programm></programm>
    <systemes><p></p></systemes>
    <systemes><table>.1.</table></systemes>
    <systemes><table>.2.</table></systemes>
    <systemes><p></p></systemes>
    <requirements></requirements>
</div>
<div>
    <programm></programm>
    <systemes><table>.1.</table></systemes>
    <systemes><p></p></systemes>
    <requirements></requirements>
</div>
<div>
    <programm></programm>
    <systemes><table>.1.</table></systemes>
    <systemes><table>.2.</table></systemes>
    <systemes><p></p></systemes>
    <requirements></requirements>
</div>
</root>

我需要输出是这样的:

<root>
<div>
    <programm></programm>
    <systemes><p></p><table>.1.</table><table>.2.</table><p></p></systemes>
    <requirements></requirements>
</div>
<div>
    <programm></programm>
    <systemes><table>.1.</table><p></p></systemes>
    <requirements></requirements>
</div>
<div>
    <programm></programm>
    <systemes><table>.1.</table><table>.2.</table><p></p></systemes>
    <requirements></requirements>
</div>
</root>

我希望有人可以帮助我解决这个问题。我知道 Muenchian 方法,但没有让它正常工作。非常感谢!

这是我到目前为止所尝试的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

<xsl:strip-space elements="*"/>

<xsl:key name="systemsKey" match="//systemes" use="name()"/>

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="systemes[generate-id()=generate-id(key('systemesKey', name())[1])]">
    <xsl:copy>
        <xsl:apply-templates select="@*|key('systemesKey', name())/node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="systemes[not(generate-id()=generate-id(key('systemesKey', name())[1]))]"/>

</xsl:stylesheet>
4

1 回答 1

0

这会产生您所描述的输出(除了元素内的systemes子元素的顺序)。

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

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="root">
  <xsl:copy>
     <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="requirements|programm|p">
  <xsl:copy>
     <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="div">
  <xsl:copy>
     <xsl:element name="systemes">
        <xsl:for-each select="systemes">
           <xsl:apply-templates/>
        </xsl:for-each>
     </xsl:element>
     <xsl:apply-templates select="requirements|programm"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="table">
  <xsl:copy>
     <xsl:value-of select="."/>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

您根本不需要使用分组,无论是否是 Muenchian。如果您想要实现的目标没有必要,我不建议您使用像键这样复杂的东西。

编辑:我使用过 XSLT 2.0,但其中没有任何内容无法在 1.0 中完成。

于 2013-11-20T09:56:23.733 回答