XSL将直接的xml结构重写为嵌套的xml [英] XSL Rewrite a straight xml structure to nested xml
问题描述
我有一个类似XML的结构:
I've a XML structure like:
<TOP>top</TOP>
<HDR>header</HDR>
<LINE>line1</LINE>
<LINE>line2</LINE>
<LINE>line3</LINE>
<HDR>header</HDR>
<LINE>line4</LINE>
<LINE>line5</LINE>
<LINE>line6</LINE>
<TOP>top</TOP>
<HDR>header</HDR>
<LINE>line1</LINE>
<LINE>line2</LINE>
<LINE>line3</LINE>
<HDR>header</HDR>
<LINE>line4</LINE>
<LINE>line5</LINE>
<LINE>line6</LINE>
我想要一个XLST脚本将其传输到:
And I want a XLST script to transfer it to:
<TOP>top</TOP>
<HDR>
header
<LINE>line1</LINE>
<LINE>line2</LINE>
<LINE>line3</LINE>
</HDR>
<HDR>
header
<LINE>line4</LINE>
<LINE>line5</LINE>
<LINE>line6</LINE>
</HDR>
<TOP>top</TOP>
<HDR>
header
<LINE>line1</LINE>
<LINE>line2</LINE>
<LINE>line3</LINE>
</HDR>
<HDR>
header
<LINE>line4</LINE>
<LINE>line5</LINE>
<LINE>line6</LINE>
</HDR>
因此:<HDR>
-标签在附近直到下一个<HDR>
-标签
Thus: The <HDR>
-tag around the LINES until next <HDR>
-tag
有人可以帮我吗,请给我xslt脚本.
Can someone help me, please to give me a xslt-script.
推荐答案
此转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kFollowing" match="LINE"
use="generate-id(preceding-sibling::HDR[1])"/>
<xsl:template match="/*">
<xsl:copy-of select="TOP"/>
<xsl:apply-templates select="HDR"/>
</xsl:template>
<xsl:template match="HDR">
<HDR>
<xsl:copy-of select="key('kFollowing', generate-id())"/>
</HDR>
</xsl:template>
</xsl:stylesheet>
应用于以下XML文档(提供的片段,包装在单个top元素中):
when applied on the following XML document (the provided fragment, wrapped into a single top element):
<t>
<TOP>top</TOP>
<HDR>header</HDR>
<LINE>line1</LINE>
<LINE>line2</LINE>
<LINE>line3</LINE>
<HDR>header</HDR>
<LINE>line4</LINE>
<LINE>line5</LINE>
<LINE>line6</LINE>
</t>
产生所需的正确结果:
<TOP>top</TOP>
<HDR>
<LINE>line1</LINE>
<LINE>line2</LINE>
<LINE>line3</LINE>
</HDR>
<HDR>
<LINE>line4</LINE>
<LINE>line5</LINE>
<LINE>line6</LINE>
</HDR>
这篇关于XSL将直接的xml结构重写为嵌套的xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!