我需要将顺序
XML节点列表转换为层次结构,但我遇到了一些特定于XSLT的知识差距.
输入XML包含文章,颜色和大小.在下面的示例中,’Record1’是一篇文章,’Record2’代表一种颜色,’Record3’代表大小.颜色和大小(record2和record3)元素的数量可以变化.
输入XML包含文章,颜色和大小.在下面的示例中,’Record1’是一篇文章,’Record2’代表一种颜色,’Record3’代表大小.颜色和大小(record2和record3)元素的数量可以变化.
<root> <Record1>...</Record1> <Record2>...</Record2> <Record3>...</Record3> <Record3>...</Record3> <Record2>...</Record2> <Record3>...</Record3> <Record3>...</Record3> <Record3>...</Record3> <Record3>...</Record3> <Record1>...</Record1> <Record2>...</Record2> <Record3>...</Record3> <Record3>...</Record3> <Record2>...</Record2> <Record3>...</Record3> <Record3>...</Record3> <Record3>...</Record3> <Record3>...</Record3> </root>
所有字段都在相同的层次结构级别,但我仍然需要创建此结构作为输出:
<root> <article> -> Record1 <color> -> Record2 <size>...</size> -> Record3 <size>...</size> -> Record3 </color> <color> -> Record2 <size>...</size> -> Record3 <size>...</size> -> Record3 <size>...</size> -> Record3 <size>...</size> -> Record3 </color> </article> <article> -> Record1 <color> -> Record2 <size>...</size> -> Record3 <size>...</size> -> Record3 </color> <color> -> Record2 <size>...</size> -> Record3 <size>...</size> -> Record3 <size>...</size> -> Record3 <size>...</size> -> Record3 </color> </article> </root>
我已经尝试按顺序迭代节点,但是例如’article'(= record1)节点标签需要保持未闭合,同时处理’color'(= record2)节点.处理’color'(= record3)处理’color’未闭合的计数相同,但XSLT不允许这样做.
我的下一个想法是为每篇文章,颜色和大小级别调用模板,但我不知道如何选择例如当前’record2’和’record1’所代表的下一篇文章之间的所有’record3’节点.
我对XSLT版本也有限制,因为我需要在BizTalk Server中进行这种转换,它只支持XSLT 1.0
有人能把我推向正确的方向吗?
这是一个XSLT 1.0选项.我不确定你想用Record1和Record2的值做什么,所以我把它们放在一个val属性中.
XML输入
<root> <Record1>article1</Record1> <Record2>color1</Record2> <Record3>size1</Record3> <Record3>size2</Record3> <Record2>color2</Record2> <Record3>size3</Record3> <Record3>size4</Record3> <Record3>size5</Record3> <Record3>size6</Record3> <Record1>article2</Record1> <Record2>color3</Record2> <Record3>size7</Record3> <Record3>size8</Record3> <Record2>color4</Record2> <Record3>size9</Record3> <Record3>size10</Record3> <Record3>size11</Record3> <Record3>size12</Record3> </root>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/*"> <xsl:copy> <xsl:apply-templates select="Record1"/> </xsl:copy> </xsl:template> <xsl:template match="Record1"> <article val="{.}"> <xsl:apply-templates select="following-sibling::Record2[generate-id(preceding-sibling::Record1[1])=generate-id(current())]"/> </article> </xsl:template> <xsl:template match="Record2"> <color val="{.}"> <xsl:apply-templates select="following-sibling::Record3[generate-id(preceding-sibling::Record2[1])=generate-id(current())]"/> </color> </xsl:template> <xsl:template match="Record3"> <size> <xsl:value-of select="."/> </size> </xsl:template> </xsl:stylesheet>
XML输出
<root> <article val="article1"> <color val="color1"> <size>size1</size> <size>size2</size> </color> <color val="color2"> <size>size3</size> <size>size4</size> <size>size5</size> <size>size6</size> </color> </article> <article val="article2"> <color val="color3"> <size>size7</size> <size>size8</size> </color> <color val="color4"> <size>size9</size> <size>size10</size> <size>size11</size> <size>size12</size> </color> </article> </root>