Splitting an xml based on the contents of xml either using java or xslt

跟風遠走 提交于 2019-12-06 12:38:51

Which XSLT 1.0 processor do you use? Xalan Java supports

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


  <xsl:template match="/Envelope/Notification/Data[not(Input/*)]">
    <redirect:write select="concat(substring(Input, 1, 6), '.xml')">
      <xsl:copy-of select="Output"/>
    </redirect:write>
  </xsl:template>

  <xsl:template match="/Envelope/Notification/Data[Input/*]">
    <redirect:write select="concat(local-name(Input/*), '.xml')">
      <xsl:copy-of select="Output"/>
    </redirect:write>
  </xsl:template>

</xsl:stylesheet>
TA Nguyen

Use the solution described in Your Previous Question you can then create 3 xslt files as follow, and apply the same xml input one for each xslt/output. Explain:

<xsl:copy-of select="/Envelope/Notification/Data[child::Output/text()='RESPONSEDATA']"/>

is saying, copy all "/Envelope/Notification/Data" and it content, but only for the child Output with a text value of 'RESPONSEDATA'.

ExtractResponseData.xslt

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

    <xsl:output indent="yes" />

    <xsl:template match="/">
        <xsl:copy-of select="/Envelope/Notification/Data[child::Output/text()='RESPONSEDATA']"/>
    </xsl:template>
</xsl:stylesheet>

ExtractNextData.xslt

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

    <xsl:output indent="yes" />

    <xsl:template match="/">
        <xsl:copy-of select="/Envelope/Notification/Data[child::Output/text()='NEXTDATA']"/>
    </xsl:template>
</xsl:stylesheet>

ExtractSomeData.xslt

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

    <xsl:output indent="yes" />

    <xsl:template match="/">
        <xsl:copy-of select="/Envelope/Notification/Data[child::Output/text()='SOMEDATA']"/>
    </xsl:template>
</xsl:stylesheet>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!