拆分重新创建XML XSLT [英] Split Recreate XML XSLT

查看:46
本文介绍了拆分重新创建XML XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将xml拆分为多个xml,但是在所有拆分的xml中必须存在一些公共元素.

I want to split the xml into multiple xml but some common elements have to be present in all the splitted xml.

输入

<xmlroot>
<FileDetails>
   <Filename>test.xml</FileName>
   <FileDate>10312014</FileDate>
</FileDetails>
 <FileInfo>
   <Test>Hello1</Test>
 </FileInfo>
 <FileInfo>
    <Test>Hello2</Test>
 </FileInfo>
 </xmlroot>

Output1

<xmlroot>
<FileDetails>
   <Filename>test.xml</FileName>
   <FileDate>10312014</FileDate>
</FileDetails>
 <FileInfo>
    <Test>Hello1</Test>
 </FileInfo>
</xmlroot>

output2

<xmlroot>
<FileDetails>
   <Filename>test.xml</FileName>
   <FileDate>10312014</FileDate>
</FileDetails>
 <FileInfo>
    <Test>Hello2</Test>
 </FileInfo>
 </xmlroot>

推荐答案

假设XSLT 2.0

Assuming XSLT 2.0

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
  <xsl:apply-templates select="*/FileInfo"/>
</xsl:template>

<xsl:template match="*/FileInfo">
  <xsl:result-document href="Output{position()}.xml">
    <xsl:apply-templates select="/*" mode="split">
      <xsl:with-param name="target" tunnel="yes" select="current()"/>
    </xsl:apply-templates>
  </xsl:result-document>
</xsl:template>

<xsl:template match="@* | node()" mode="#all">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()" mode="#current"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*/FileInfo" mode="split">
  <xsl:param name="target" tunnel="yes"/>
  <xsl:if test="$target is .">
    <xsl:next-match/>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

如果您需要在XSLT 1.0中使用Xalan Java解决方案,但需要使用特定于Xalan的扩展元素,那么这里是一个示例:

If you need a solution with Xalan Java in XSLT 1.0 but making use of a Xalan specific extension element then here is an example:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:redirect="http://xml.apache.org/xalan/redirect"
  extension-element-prefixes="redirect"
  exclude-result-prefixes="redirect"
  version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
  <xsl:apply-templates select="*/FileInfo" mode="split"/>
</xsl:template>

<xsl:template match="*/FileInfo" mode="split">
  <redirect:write file="OutputXalanTest{position()}.xml">
    <xsl:apply-templates select="/*">
      <xsl:with-param name="target" select="current()"/>
    </xsl:apply-templates>
  </redirect:write>
</xsl:template>

<xsl:template match="@* | node()" name="identity">
  <xsl:param name="target"/>
  <xsl:copy>
    <xsl:apply-templates select="@* | node()">
      <xsl:with-param name="target" select="$target"/>
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="*/FileInfo">
  <xsl:param name="target"/>
  <xsl:if test="generate-id($target) = generate-id(.)">
    <xsl:call-template name="identity"/>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

我仅使用Apache的Xalan版本进行了测试,但希望它能与Java JRE中包含的Xalan版本Sun/Oracle一起使用.

I have only tested that with the Xalan version available from Apache but hopefully it works as well with the Xalan version Sun/Oracle incorporated in the Java JRE.

这篇关于拆分重新创建XML XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆