R
R. P.
Subject: XSLT to transform a flat XML file into a structured text file
I have an XML file that lists the PDF file segment names and titles of a
larger document and looks something like this:
<DOCUMENT>
......
...... some lead elements
......
<SEGMENT_LIST>
<SEGMENT FILE="fwd.pdf">Foreword</SEGMENT>
<SEGMENT FILE="chap1.pdf">Chapter 1</SEGMENT>
<SEGMENT FILE="chap2.pdf">Chapter 2</SEGMENT>
<SEGMENT FILE="chap3.pdf">Chapter 3</SEGMENT>
<SEGMENT FILE="v1fwd.pdf" VOLUME="Volume 1">Foreword</SEGMENT>
<SEGMENT FILE="v1defs.pdf" VOLUME="Volume 1">Definitions</SEGMENT>
<SEGMENT FILE="v1meth.pdf" VOLUME="Volume 1">Methodology</SEGMENT>
<SEGMENT FILE="v1sachap1.pdf" VOLUME="Volume 1" GROUP='Section
A">Chapter 1A</SEGMENT>
<SEGMENT FILE="v1sachap2.pdf" VOLUME="Volume 1" GROUP='Section
A">Chapter 2A</SEGMENT>
<SEGMENT FILE="v1sachap3.pdf" VOLUME="Volume 1" GROUP='Section
A">Chapter 3A</SEGMENT>
<SEGMENT FILE="v1sbchap1.pdf" VOLUME="Volume 1" GROUP='Section
B">Chapter 1B</SEGMENT>
<SEGMENT FILE="v1sbchap2.pdf" VOLUME="Volume 1" GROUP='Section
B">Chapter 2B</SEGMENT>
<SEGMENT FILE="v1sbchap3.pdf" VOLUME="Volume 1" GROUP='Section
B">Chapter 3B</SEGMENT>
<SEGMENT FILE="appa.pdf" GROUP="Appendices">Appendix A</SEGMENT>
<SEGMENT FILE="appb.pdf" GROUP="Appendices">Appendix B</SEGMENT>
<SEGMENT FILE="appc.pdf" GROU2P="Appendices">Appendix C</SEGMENT>
</SEGMENT_LIST>
</DOCUMENT>
I need to transform the SEGMENT_LIST elements into a structured text
file for use by another application to construct the Table Of Content
(TOC). The file would be vertical bar (|) separated list of PDF file
segment names and their titles with a single-digit TOC indentation level
indicator in the first position as so:
1|fwd.pdf|Foreword
1|chap1.pdf|Chapter 1
1|chap2.pdf|Chapter 2
1|chap3.pdf|Chapter 3
1||Volume 1
2|v1fwd.pdf|Foreword
2|v1defs.pdf|Definitions
2|v1meth.pdf|Methodology
2||Section A
3|v1sachap1.pdf|Chapter 1A
3|v1sachap2.pdf|Chapter 2A
3|v1sachap3.pdf|Chapter 3A
2||Section B
3|v1sbchap1.pdf|Chapter 1B
3|v1sbchap2.pdf|Chapter 2B
3|v1sbchap3.pdf|Chapter 3B
1||Appendices
2|appa.pdf|Appendix A
2|appb.pdf|Appendix B
2|appc.pdf|Appendix C
I think you can imagine from the transformed file how the TOC would look
like:
Foreword
Chapter 1
Chapter 2
Chapter 3
Volume 1
Foreword
Definitions
Methodology
Section A
Chapter 1A
Chapter 2A
Chapter 3A
Section B
Chapter 1B
Chapter 2B
Chapter 3B
Appendices
Appendix A
Appendix B
Appendix C
My problem is that while I find it easy to write an XSLT stylesheet to
create the first 4 lines of the output file where the source XML does
not have either of the optional VOLUME and GROUP attributes:
<xsl:template match="/">
<xsl:apply-templates select="/DOCUMENT/SEGMENT_LIST/*" />
</xsl:template>
<xsl:template match="SEGMENT">
<xsl:text>1|</xsl:text>
<xsl:value-of select="@FILE"/>
<xsl:text>|</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
I have no idea however, how to transform the rest of XML because I don't
know how to process those attributes to make them Volume, Section and
Appendices headers in the output file for all the segments with the
same attribute value and with the proper indent level numbers.
Any suggestion would be greatly appreciated.
Rudy
I have an XML file that lists the PDF file segment names and titles of a
larger document and looks something like this:
<DOCUMENT>
......
...... some lead elements
......
<SEGMENT_LIST>
<SEGMENT FILE="fwd.pdf">Foreword</SEGMENT>
<SEGMENT FILE="chap1.pdf">Chapter 1</SEGMENT>
<SEGMENT FILE="chap2.pdf">Chapter 2</SEGMENT>
<SEGMENT FILE="chap3.pdf">Chapter 3</SEGMENT>
<SEGMENT FILE="v1fwd.pdf" VOLUME="Volume 1">Foreword</SEGMENT>
<SEGMENT FILE="v1defs.pdf" VOLUME="Volume 1">Definitions</SEGMENT>
<SEGMENT FILE="v1meth.pdf" VOLUME="Volume 1">Methodology</SEGMENT>
<SEGMENT FILE="v1sachap1.pdf" VOLUME="Volume 1" GROUP='Section
A">Chapter 1A</SEGMENT>
<SEGMENT FILE="v1sachap2.pdf" VOLUME="Volume 1" GROUP='Section
A">Chapter 2A</SEGMENT>
<SEGMENT FILE="v1sachap3.pdf" VOLUME="Volume 1" GROUP='Section
A">Chapter 3A</SEGMENT>
<SEGMENT FILE="v1sbchap1.pdf" VOLUME="Volume 1" GROUP='Section
B">Chapter 1B</SEGMENT>
<SEGMENT FILE="v1sbchap2.pdf" VOLUME="Volume 1" GROUP='Section
B">Chapter 2B</SEGMENT>
<SEGMENT FILE="v1sbchap3.pdf" VOLUME="Volume 1" GROUP='Section
B">Chapter 3B</SEGMENT>
<SEGMENT FILE="appa.pdf" GROUP="Appendices">Appendix A</SEGMENT>
<SEGMENT FILE="appb.pdf" GROUP="Appendices">Appendix B</SEGMENT>
<SEGMENT FILE="appc.pdf" GROU2P="Appendices">Appendix C</SEGMENT>
</SEGMENT_LIST>
</DOCUMENT>
I need to transform the SEGMENT_LIST elements into a structured text
file for use by another application to construct the Table Of Content
(TOC). The file would be vertical bar (|) separated list of PDF file
segment names and their titles with a single-digit TOC indentation level
indicator in the first position as so:
1|fwd.pdf|Foreword
1|chap1.pdf|Chapter 1
1|chap2.pdf|Chapter 2
1|chap3.pdf|Chapter 3
1||Volume 1
2|v1fwd.pdf|Foreword
2|v1defs.pdf|Definitions
2|v1meth.pdf|Methodology
2||Section A
3|v1sachap1.pdf|Chapter 1A
3|v1sachap2.pdf|Chapter 2A
3|v1sachap3.pdf|Chapter 3A
2||Section B
3|v1sbchap1.pdf|Chapter 1B
3|v1sbchap2.pdf|Chapter 2B
3|v1sbchap3.pdf|Chapter 3B
1||Appendices
2|appa.pdf|Appendix A
2|appb.pdf|Appendix B
2|appc.pdf|Appendix C
I think you can imagine from the transformed file how the TOC would look
like:
Foreword
Chapter 1
Chapter 2
Chapter 3
Volume 1
Foreword
Definitions
Methodology
Section A
Chapter 1A
Chapter 2A
Chapter 3A
Section B
Chapter 1B
Chapter 2B
Chapter 3B
Appendices
Appendix A
Appendix B
Appendix C
My problem is that while I find it easy to write an XSLT stylesheet to
create the first 4 lines of the output file where the source XML does
not have either of the optional VOLUME and GROUP attributes:
<xsl:template match="/">
<xsl:apply-templates select="/DOCUMENT/SEGMENT_LIST/*" />
</xsl:template>
<xsl:template match="SEGMENT">
<xsl:text>1|</xsl:text>
<xsl:value-of select="@FILE"/>
<xsl:text>|</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:template>
I have no idea however, how to transform the rest of XML because I don't
know how to process those attributes to make them Volume, Section and
Appendices headers in the output file for all the segments with the
same attribute value and with the proper indent level numbers.
Any suggestion would be greatly appreciated.
Rudy