首页 文章

XSLT使用升序添加父级并使用属性分组

提问于
浏览
1

我有一些XML,我需要将父节点添加到node1,node2,node3等 . 然后我还需要与其他节点分组:

原始XML:

<parentnode>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
<childnode attribute="option.a.b.3">
</childnode>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
</parentnode>

期望的XML:

<parentnode>
<row0>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
</row0>
<row1>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
<childnode attribute="option.a.b.3">
</childnode>
</row1>
<row2>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
</row2>
</parentnode>

option.a.b . * * *可以是任何数字,只要每次出现option.a.b.1,我都需要它来开始一个新行 . 我甚至不确定这是否可以在XSLT中使用?

2 回答

  • 1

    每次出现option.a.b.1时,我只需要它开始一个新行 . 我甚至不确定这是否可以在XSLT中使用?

    XSLT - 甚至XSLT 1.0 - 是一种图灵完备语言,所以是的,它是可能的 . 如果您使用的是XSLT 1.0,请尝试:

    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:key name="k" match="childnode[not(@attribute='option.a.b.1')]" use="generate-id(preceding-sibling::childnode[@attribute='option.a.b.1'][1])" />
    
    <xsl:template match="/parentnode">
        <xsl:copy> 
            <xsl:for-each select="childnode[@attribute='option.a.b.1']">
                <xsl:element name="row{position()-1}">
                    <xsl:copy-of select=". | key('k', generate-id())"/>
                </xsl:element>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Note :你可以通过调整我在这里给你的答案来解决这个问题:https://stackoverflow.com/a/26397156/3016153

  • 1

    假设你可以使用像Saxon 9或XmlPrime或AltovaXML这样的XSLT 2.0处理器

    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="parentnode">
            <xsl:copy>
                <xsl:for-each-group select="childnode" group-starting-with="childnode[@attribute = 'option.a.b.1']">
                    <row>
                        <xsl:copy-of select="current-group()"/>
                    </row>
                </xsl:for-each-group>
            </xsl:copy>
        </xsl:template>
    
    </xsl:transform>
    

    我故意不对行元素进行编号,因为如果你真的需要那么我的视图中会出现格式不佳的情况

    <xsl:template match="parentnode">
        <xsl:copy>
            <xsl:for-each-group select="childnode" group-starting-with="childnode[@attribute = 'option.a.b.1']">
                <xsl:element name="row{position() - 1}">
                    <xsl:copy-of select="current-group()"/>
                </xsl:element>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
    

相关问题