English 中文(简体)
如何修正此 XSLT 将 XML 节点分组到有特定属性的父节点?
原标题:How to fix this XSLT to group this XML node to parent that has specific attribute?
  • 时间:2012-05-26 14:52:02
  •  标签:
  • xml
  • xslt

我需要将此节点合并到一起, 但是它必须和具有属性 method=" a" 的母节点合并到一起 。

输入 :

<myroot>
    <elem name="a" creationDate="">
        <list id="xxx" ver="uu">
            <nodeA id="a">
                <fruit id="small">
                    <orange id="x" method="create">                       
                        <color>Orange</color>                                               
                    </orange>
                </fruit>     
                <fruit id="small" method="a">
                    <kiwi id="y" method="create">                        
                        <color>Red</color>
                        <type>sour</type>                       
                    </kiwi>                    
                </fruit>
                <fruit id="large" method="a">
                    <melon id="y" method="create">
                        <attributes>
                            <color>Green</color>
                            <type>sweet</type>
                        </attributes>
                    </melon>                    
                </fruit>
            </nodeA>
        </list>
    </elem>
</myroot>

我的 XSL 文件 :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="list">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:for-each-group select="/*/*/*/*" group-by="@id">
                <xsl:copy>
                    <xsl:copy-of select="@*"/>
                    <xsl:for-each-group select="current-group()/*" group-by="concat(local-name(),  | , @id)">
                        <xsl:copy>
                            <xsl:apply-templates select="@*, *, (current-group() except .)/*"/>
                        </xsl:copy>
                    </xsl:for-each-group>
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

<强 > 我的输出:

<myroot>
    <elem name="a" creationDate="">
        <list id="xxx" ver="uu">
            <nodeA id="a">
                <fruit id="small">            
                    <orange id="x" method="create">                       
                        <color>Orange</color>                                               
                    </orange>
                    <kiwi id="y" method="create">                        
                        <color>Red</color>
                        <type>sour</type>                       
                    </kiwi>                    
                </fruit>     

                <fruit id="large" method="a">
                    <melon id="y" method="create">
                        <attributes>
                            <color>Green</color>
                            <type>sweet</type>
                        </attributes>
                    </melon>                    
                </fruit>
            </nodeA>
        </list>
    </elem>
</myroot>

<强 > 预期输出:

<myroot>
    <elem name="a" creationDate="">
        <list id="xxx" ver="uu">
            <nodeA id="a">
                <fruit id="small" method="a"> <!-- this is the correct merge where the merged is in parent that has a method -->
                    <kiwi id="y" method="create">                        
                        <color>Red</color>
                        <type>sour</type>                       
                    </kiwi>
                    <orange id="x" method="create">                       
                        <color>Orange</color>                                               
                    </orange>
                </fruit>     

                <fruit id="large" method="a">
                    <melon id="y" method="create">
                        <attributes>
                            <color>Green</color>
                            <type>sweet</type>
                        </attributes>
                    </melon>                    
                </fruit>
            </nodeA>
        </list>
    </elem>
</myroot>

我们可以看到我的变换只将它组合在一起而不考虑方法。 如何修改它, 以便将其合并到有 ethod=" a" 的母体, 示例是

Thank you. John

最佳回答

元素中,上下文项目只是该组的第一个元素, 所以 select="\\\\\\\" 将复制第一个元素的属性。 要获取所有不同名称属性的复制件, 您需要访问的任何元素中, 获取 procety- group ()/ {/ {/ code> 。

也没有必要 select=" * (当前组 () 除外.) / * " , 因为它相当于 select= "当前组 ()/ *"

完整样式表看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <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="list">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates select="*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="list/*">

      <xsl:copy>

        <xsl:copy-of select="@*" />

        <xsl:for-each-group select="*" group-by="concat(local-name(),  | , @id)">
          <xsl:copy>
            <xsl:copy-of select="current-group()/@*" />
            <xsl:copy-of select="current-group()/*" />
          </xsl:copy>
        </xsl:for-each-group>

      </xsl:copy>

  </xsl:template>

</xsl:stylesheet>
问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml