0

I have to transform xml file with few attributes under a root to XML using xslt i could achive that for single root occurence but my input xml will contain multiple records(occurences of root & its subnodes with different values) how to add looping so that after completing parsing of first root goes to its next accourence. Below is the input XML

Input XML

<Information>
    <Name>Joe</Name>
    <DOB>01012014</DOB>
</Information>
<Information>
    <Name>Mark</Name>
    <DOB>12012012</DOB>
</Information>

XSLT:

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

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <xsl:element name="Information">
            <xsl:apply-templates select="Information/Name"/>
            <xsl:apply-templates select="Information/DOB"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="Information/Name">
        <xsl:element name="Name">
            <xsl:value-of select="."/>  
        </xsl:element>
    </xsl:template>

    <xsl:template match="Information/DOB">
        <xsl:element name="DOB">
            <xsl:value-of select="."/>  
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Output: Basically need to reorder DOB first then name and process both the root nodes information

<Information>
    <DOB>01012014</DOB>
    <Name>Joe</Name>
</Information>
<Information>
    <DOB>12012012</DOB>
    <Name>Mark</Name>
</Information>

Any help greatly appreciated.

helderdarocha
  • 23,209
  • 4
  • 50
  • 65
  • You need to pre-process your input source in order to add a root node wrapping your elements, since that the way it is it's not well-formed and will not be accepted as XSLT input. – helderdarocha May 01 '14 at 00:46
  • You could also create a "wrapper" XML file that includes your XML fragment by using an entity reference: http://stackoverflow.com/a/5127928/14419 – Mads Hansen May 01 '14 at 01:05
  • @MadsHansen This is an interesting idea, but how would you point the wrapper to the document to process? – michael.hor257k May 01 '14 at 11:09
  • @michael.hor257k in the declaration of the entity you provide the path to the file. Whether that is a relative path, URL, etc. Just one option for producing a well-formed document from the fragment. – Mads Hansen May 01 '14 at 11:40
  • @MadsHansen I understand that - I just don't see how you would do this dynamically, given that the fragment document (presumably) changes for each transformation. – michael.hor257k May 01 '14 at 12:12
  • A few options come to mind. Depending on how often this needs to be done and what other technologies available. For one-off, hand code or copy file and use static file name. For a pure XSLT 2.0 solution, you could use collection() to discover the fragments, result-document() to generate the "wrapper" docs, load the generated wrapper with document(), and then transform. – Mads Hansen May 01 '14 at 12:32
  • For XSLT 3.0 you could probably load with parse-xml-fragment() – Mads Hansen May 01 '14 at 12:34
  • @MadsHansen It seems to me more trouble than it's worth. In XSLT 2.0, you could pass the path to the document as a parameter to a stylesheet to load as unparsed-text, add a root element and save it as an XML document to be processed in step 2. Of course, you could do the same thing using another method e.g. text file manipulation - the point is that the file to process would be one of the inputs to the pipeline. – michael.hor257k May 01 '14 at 16:50

1 Answers1

2

my input xml will contain multiple records

If it contains multiple top-level elements then it isn't XML any more.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164