Question

I've the below xml files

XML1:

<chapter num="01">
<title><page num="1"/><content-style font-style="bold">INTRODUCTION</content-style></title>
<chapter-meta><author><name><firstname>YEO</firstname><middlename>HWEE</middlename><surname>YING</surname></name></author></chapter-meta>
</chapter>

XML2:

<chapter num="02">
<title><page num="35"/><content-style font-style="bold">INCORPORATION AND ITS CONSEQUENCES</content-style></title>
<chapter-meta><author><name><firstname>TAN</firstname><middlename>CHENG</middlename><surname>HAN</surname></name></author></chapter-meta>
</chapter>

XML3:

<chapter num="03">
<title><page num="78"/><page num="79"/><content-style font-style="bold">CORPORATE PERSONALITY</content-style></title>
<chapter-meta><author><name><firstname>TAN</firstname><middlename>CHENG</middlename><surname>HAN</surname></name></author></chapter-meta>
</chapter>

and the main XML file is

<main>
<toc-item>
<toc-title>CHAPTER 1 INTRODUCTION</toc-title>
</toc-item>
<toc-item>
<toc-title>CHAPTER 2 INCORPORATION AND ITS CONSEQUENCES</toc-title>
</toc-item>
<toc-item>
<toc-title>CHAPTER 3 CORPORATE PERSONALITY</toc-title>
</toc-item>
</main>

here i need to compare the chapter numbers with the chapter number in the xml files (xml1, xml2, xml3) and display the author name. The expected output is as below.

Chapter 1: YEO HWEE YING
Chapter 2: TAN CHENG HAN
Chapter 3: TAN CHENG HAN

I've tried the below code.

  <xsl:variable name="pg">
                                     <xsl:value-of select="document('C:\Users\u0138039\Desktop\Proview\SG\Commentary_SG_XML-03032014\SG-Walter Woon on Company Law (3rd ed)/title.xml')/entry/file/translate(translate(./@name,'Walter Woon on Company Law (3rd ed)_Chapter',''),'.x  ','')"/>                                                                              </xsl:variable>
                            <xsl:value-of select="$Chapn=$pg"/>

here i have crated an XML file with all the chapter names and took that in the document element. here when i try to print the output, it is showing me false.

please let me know how to do this.

Thanks

Was it helpful?

Solution

With most XSLT 2.0 processors you can use the collection function to access a collection of files from the file system but the concrete syntax depends on the XSLT processor. With Saxon 9 the following works for me:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="chapters" select="collection('.?select=XML*.xml')/chapter"/>

<xsl:template match="toc-item/toc-title">
  <xsl:analyze-string select="." regex="chapter\s+([0-9]+)\s+" flags="i">
    <xsl:matching-substring>
      <xsl:value-of select="."/>
      <xsl:value-of select="$chapters[xs:integer(@num) eq xs:integer(regex-group(1))]/chapter-meta/author/name/*"/>
      <xsl:text>&#10;</xsl:text>
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

With your three files XML1.xml, XML2.xml, XML3.xml in the same working directory as the main input the output then is

CHAPTER 1 YEO HWEE YING
CHAPTER 2 TAN CHENG HAN
CHAPTER 3 TAN CHENG HAN

With the version 2013 of AltovaXML the following works for me:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="chapters" select="collection('./XML*.xml')/chapter"/>

<xsl:template match="toc-item/toc-title">
  <xsl:analyze-string select="." regex="chapter\s+([0-9]+)\s+" flags="i">
    <xsl:matching-substring>
      <xsl:value-of select="."/>
      <xsl:value-of select="$chapters[xs:integer(@num) eq xs:integer(regex-group(1))]/chapter-meta/author/name/*"/>
      <xsl:text>&#10;</xsl:text>
    </xsl:matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

I don't know whether that use of collection works with the latest version called Raptor of Altova's XSLT engine, I don't have access to that as they don't provide a developer release or license anymore and http://manual.altova.com/RaptorXML/raptorxmlserver/ does not seem to contain a documentation of the use of collection, at least on quick browsing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top