Xml 使用XSLT获取前一个同级的最后一组
我需要将Xml 使用XSLT获取前一个同级的最后一组,xml,xslt,Xml,Xslt,我需要将元素、和元素(如果可用)拉入元素 以下是一个例子: 发件人: <root> <amendment> <id>1</id> <text>some text</text> </amendment> <sectionid>A</sectionid> <sponsor>jon<
元素、
和
元素(如果可用)拉入
元素
以下是一个例子:
发件人:
<root>
<amendment>
<id>1</id>
<text>some text</text>
</amendment>
<sectionid>A</sectionid>
<sponsor>jon</sponsor>
<sponsor>peter</sponsor>
<amendment>
<id>8</id>
<text>some text</text>
</amendment>
<sectionid>B</sectionid>
<sponsor>matt</sponsor>
<sponsor>ben</sponsor>
<amendmenttext>some intro text</amendmenttext>
<amendment>
<id>5</id>
<text>some text</text>
</amendment>
<amendment>
<id>4</id>
<text>some text</text>
</amendment>
<sponsor>max</sponsor>
<amendment>
<id>6</id>
<text>some text</text>
</amendment>
<amendment>
<id>7</id>
<text>some text</text>
</amendment>
</root>
1.
一些文本
A.
乔恩
彼得
8.
一些文本
B
马特
本
一些介绍文字
5.
一些文本
4.
一些文本
最大值
6.
一些文本
7.
一些文本
致:
1.
一些文本
A.
乔恩
彼得
8.
一些文本
B
马特
本
一些介绍
5.
一些文本
B
马特
本
4.
一些文本
B
最大值
6.
一些文本
B
最大值
7.
一些文本
注1:
元素适用于下一个
注2:
元素适用于下一个
列表之前的所有
注3://amendment/id
的值不是顺序的
注4:
不能将
或
作为以前的兄弟姐妹
注5:
仅适用于以下
如何使用XSLT 1.0完成此转换。此样式表(答案与以前相同,但只有一条规则):
输出:
<root>
<amendment>
<id>1</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>A</sectionid>
<sponsor>jon</sponsor>
<sponsor>peter</sponsor>
<id>8</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>matt</sponsor>
<sponsor>ben</sponsor>
<id>5</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>matt</sponsor>
<sponsor>ben</sponsor>
<id>4</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>max</sponsor>
<id>6</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>max</sponsor>
<id>7</id>
<text>some text</text>
</amendment>
</root>
1.
一些文本
A.
乔恩
彼得
8.
一些文本
B
马特
本
5.
一些文本
B
马特
本
4.
一些文本
B
最大值
6.
一些文本
B
最大值
7.
一些文本
注意:与上一个答案的差异是这些规则中需要的参数的默认空节点集表达式。对上一个问题的修改是次要的。我认为这不值得再问一个问题。不知道解决方案会是类似的。我认为在最初的回答者回答后改变问题的背景对他们来说是不公平的。但我要说的是。@Alejandro,我刚刚添加到示例XML中。@Benjamin Ortuzar:此时,您应该知道如何相应地更改此答案或之前的答案。让我们看看此样式表中的修改:添加参数
pAmendmentText
并将空节点集/..
作为defaut值,添加变量vAmendmentText
作为self::amendmenttest
,将此变量添加到xsl:if/@test
以跳过复制,用$pAmendmentText[$vAmendment]
添加xsl:code>指令副本,用$pAmendmentText[非($vAmendment)]传递pAmendmentText
参数
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:param name="pSectionId" select="/.."/>
<xsl:param name="pSponsors" select="/.."/>
<xsl:variable name="vSectionid" select="self::sectionid"/>
<xsl:variable name="vSponsor" select="self::sponsor"/>
<xsl:variable name="vAmendment" select="self::amendment"/>
<xsl:if test="not($vSectionid|$vSponsor)">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="$pSectionId[$vAmendment]"/>
<xsl:copy-of select="$pSponsors[$vAmendment]"/>
<xsl:apply-templates select="node()[1]"/>
</xsl:copy>
</xsl:if>
<xsl:apply-templates select="following-sibling::node()[1]">
<xsl:with-param name="pSectionId"
select="$pSectionId[not($vSectionid)]|$vSectionid"/>
<xsl:with-param name="pSponsors"
select="$pSponsors[not($vSponsor) or
current()
/preceding-sibling::node()[1]
/self::sponsor] |
$vSponsor"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
<root>
<amendment>
<id>1</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>A</sectionid>
<sponsor>jon</sponsor>
<sponsor>peter</sponsor>
<id>8</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>matt</sponsor>
<sponsor>ben</sponsor>
<id>5</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>matt</sponsor>
<sponsor>ben</sponsor>
<id>4</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>max</sponsor>
<id>6</id>
<text>some text</text>
</amendment>
<amendment>
<sectionid>B</sectionid>
<sponsor>max</sponsor>
<id>7</id>
<text>some text</text>
</amendment>
</root>