XSLT 添加具有升序数字的父项并使用属性进行分组
XSLT add a parent with ascending numbers and grouping with attribute
我有一些 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 中是否可行?
假设您可以使用像 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>
I just need it to start a new row every time option.a.b.1 appears. I'm
not even sure if this is this possible in 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>
注意:你可以通过调整我在这里给你的答案来解决这个问题:
我有一些 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 中是否可行?
假设您可以使用像 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>
I just need it to start a new row every time option.a.b.1 appears. I'm not even sure if this is this possible in 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>
注意:你可以通过调整我在这里给你的答案来解决这个问题: