Question

I have a requirement,consider the below xml data Input1.xml

 <Envelope>
    <Notification>
    <Data>
    <Input>ABCDEFGHIJKLMN</Input>
    <Output>RESPONSEDATA</Output>
    </Data>
  <Data>
    <Input>OPQRSTUVWXYZ</Input>
    <Output>NEXTDATA</Output>
    </Data>
  <Data>
    <Input>ALPHABETS</Input>
    <Output>SOMEDATA</Output>
    </Data>
    </Notification>
    </Envelope>

Now I want 3 output xmls with the response as shown below the file name to have first 6 characters ofABCDEFGHIJKLMN as output file name to have as shown below

(FILE1)->ABCDEF.XML
    <Output>RESPONSEDATA</Output>
(FILE2)->OPQRST.XML

    <Output>NEXTDATA</Output>
(FILE3)->ALPHAB.XML
 <Output>SOMEDATA</Output>
Was it helpful?

Solution

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>

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top