我需要使用XSL在xml中的两个元素之间提取少量元素,并将提取的内容写入xml文件。
我有以下XML。
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element_1>
<h1>title</h1>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<table>
<tr>
<td />
</tr>
</table>
<h1>Another Title</h1>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<table>
<tr>
<td/>
</tr>
</table>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<table>
<tr>
<td/>
</tr>
</table>
<h1>Some other Title</h1>
<p>paragraph</p>
<table>
<tr>
<td/>
</tr>
</table>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<table>
<tr>
<td/>
</tr>
</table>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
</element_1>
<element_2/>
</root>
XSL对我来说是新手,在使用提取的内容生成新文件时遇到困难。不知何故,我能够管理生成新文件但无法在两个特定()标记之间提取标记。上面的XML是第三方工具的输出。
请分享您的想法或者是否有人想要在标签之间提取元素?
预期输出应如下所示:
File1.xml:
<modified>
<h1>title</h1>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<table><tr><td/></tr></table>
</modified>
File2.xml:
<modified>
<h1>Another Title</h1>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<table><tr><td/></tr></table>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<table><tr><td/></tr></table>
</modified>
File3.xml:
<modified>
<p>paragraph</p>
<table><tr><td/></tr></table>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
<table><tr><td/></tr></table>
<p>paragraph</p>
<p>paragraph</p>
<p>paragraph</p>
</modified>
谢谢。
答案 0 :(得分:1)
您可以使用以下样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="element_1">
<xsl:for-each-group select="*" group-starting-with="h1">
<xsl:result-document href="{concat('File', count(preceding-sibling::h1) + 1, '.xml')}">
<modified>
<xsl:apply-templates select="current-group()"/>
</modified>
</xsl:result-document>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>