我有下面的XML结构拆分XML文件复制头 - XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<ExportData>
<TransportHeader>
<Timestamp>2011-01-16 06:00:33</Timestamp>
<From>
<Name>DynamicExport</Name>
<Version>1.</Version>
</From>
<MessageId>d7b5c5b69a83</MessageId>
</TransportHeader>
<ExportConfig>
<DateTimeFormat>yyyy-MM-dd HH:mm:ss</DateTimeFormat>
<DecimalSymbol>.</DecimalSymbol>
</ExportConfig>
<DataSet>
<Tables>
<Table>
<RH>...</RH>
<Rows>
<R>Data1</R>
<R>Data2</R>
<R>Data3</R>
<R>Data4</R>
<R>Data5</R>
</Rows>
</Table>
</Tables>
</DataSet>
</ExportData>
和我需要根据<R>
元素的量来分割该文件。 如果有多于3个<R>
元素,则需要生成第二个输出文件。这两个文件也需要标题信息。
我想出了这个XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:redirect="http://xml.apache.org/xalan/redirect"
extension-element-prefixes="redirect"
exclude-result-prefixes="xd"
version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text() | @*"/>
<xsl:template match="Rows" name="Rows">
<Rows>
<xsl:for-each select="R">
<xsl:variable name="filename1" select="concat('output1','.xml')"/>
<xsl:variable name="filename2" select="concat('output2','.xml')"/>
<xsl:variable name="nodePosition" select="position()" />
<xsl:if test="$nodePosition < 3">
<redirect:write select="$filename1">
<xsl:copy-of select="." />
</redirect:write>
</xsl:if>
<xsl:if test="$nodePosition = 3 or $nodePosition > 3">
<redirect:write select="$filename2">
<xsl:copy-of select="." />
</redirect:write>
</xsl:if>
</xsl:for-each>
</Rows>
</xsl:template>
</xsl:stylesheet>
但是获取生成只包含“数据2”和“数据5”两个输出文件。 你能帮我弄清楚为什么其他3个数据元素丢失了吗? 如何添加标题数据?
对于标头,我想出了这个XSLT:当我把它应用到所提到的XML
<xsl:template match="//Rows">
<xsl:apply-templates select="@*|Rows"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
其中工程。但是我无法将这2个XSLT合并在一起 - 输出结果就搞砸了。
非常感谢您的帮助! 祝你好运, 彼得
提供的XSLT代码甚至不是格式良好的XML。 'redirect:'前缀不绑定到任何名称空间。请改正。您似乎正在使用某种扩展元素来生成多个输出。你使用什么扩展库? – 2011-05-03 13:33:23
另外,''指令缺少名称空间定义和至少一个其他必要的属性。请提供可执行的代码 - 用于复制目的,不要混淆读者。 –
2011-05-03 13:35:03
你好Dimitre,谢谢你的回答。我纠正了我的错误代码。我使用XALAN作为处理器,但我不知道扩展库。 – Peter 2011-05-03 14:03:35