[英]XSLT 1.0 Logic to split elements
The Loop-1,2 and 3 elements are unbounded.below is the input xml which has all the LOOP-1 elements comes first and then Loop-2 elements. Loop-1,2和3元素是无界的。下面是输入xml,它具有所有LOOP-1元素,然后是Loop-2元素。 how we can transform that into like output xml.I tried below xslt but it is giving as same input. 我们如何将其转换成类似输出的xml。我在xslt下尝试过,但它提供了相同的输入。
input xml: 输入xml:
<root>
<LOOP-1><!-- unbounded -->
<element1>A</element1>
</LOOP-1>
<LOOP-1>
<element1>A</element1>
</LOOP-1>
<LOOP-2><!-- unbounded -->
<element2>B</element2>
</LOOP-2>
<LOOP-2>
<element2>B</element2>
</LOOP-2>
</root>
XSLT: XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
output: 输出:
<?xml version="1.0"?>
<root>
<LOOP-1>
<element1>A</element1>
</LOOP-1>
<LOOP-2>
<element2>B</element2>
</LOOP-2>
<LOOP-1>
<element1> A</element1>
</LOOP-1>
<LOOP-2>
<element2>B</element2>
</LOOP-2>
</root>
You can try the code below: 您可以尝试以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root">
<xsl:for-each select="child::LOOP-1">
<xsl:variable name="pos" select="position()"/>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::LOOP-2[position()=$pos]"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
If you want to cope with any number of LOOP elements (You mention 1,2 and 3 in the first sentence, but don't actually show 3 in your example), what you could do is define a key to help the looking up of the elements based on their 'number'. 如果要处理任何数量的LOOP元素(在第一句中提到了1,2和3,但在示例中并未实际显示3),则可以做的是定义一个键来帮助查找基于其“数量”的元素。
<xsl:key name="loop" match="*[starts-with(local-name(), 'LOOP')]" use="substring-after(local-name(), '-')" />
You would then start off by selecting only the LOOP-1 elements 然后,您将仅选择LOOP-1元素
<xsl:apply-templates select="key('loop', '1')" />
Next, you would have a share template to match any LOOP element, regardless of number 接下来,您将拥有一个共享模板来匹配任何LOOP元素,无论数量如何
Within this you would call the identity template to copy the element, followed by an apply-templates to select the LOOP element with the next number, but one in the same position: 在此方法中,您将调用身份模板来复制元素,然后调用apply-templates选择具有下一个数字但位置相同的LOOP元素:
<xsl:apply-templates select="key('loop', string(number(substring-after(local-name(), '-')) + 1))[$pos]" />
Try this XSLT 试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
<xsl:key name="loop" match="*[starts-with(local-name(), 'LOOP')]" use="substring-after(local-name(), '-')" />
<xsl:template match="root">
<root>
<xsl:apply-templates select="key('loop', '1')" />
</root>
</xsl:template>
<xsl:template match="*[starts-with(local-name(), 'LOOP')]">
<xsl:call-template name="identity" />
<xsl:variable name="pos" select="position()" />
<xsl:apply-templates select="key('loop', string(number(substring-after(local-name(), '-')) + 1))[$pos]" />
</xsl:template>
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.