Subject: Re: [xsl] Is split problem a grouping problem? From: "Terry Badger terry_badger@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Fri, 2 Feb 2018 02:12:55 -0000 |
Rick, From the rules this does not look like a grouping problem but rather each section drives a file and get what it needs. Think this is close. Fro <?xml version="1.0" encoding="UTF-8"?> <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"> B B B B <xsl:template match="book"> B B B B B B B B <wrapper> B B B B B B B B B B B B <xsl:apply-templates select="chapter/section"/> B B B B B B B B </wrapper> B B B B </xsl:template> B B B B <!-- each section drives out a file and picks up what it needs to have --> B B B B <xsl:template match="section"> B B B B B B B B <xsl:choose> B B B B B B B B B B B B <!-- has intro --> B B B B B B B B B B B B <xsl:when test=".[intro]"> B B B B B B B B B B B B B B B B <xsl:element name="file"> B B B B B B B B B B B B B B B B B B B B <xsl:copy-of select="parent::chapter/title"/> B B B B B B B B B B B B B B B B B B B B <xsl:if test="parent::section and position() = 1"> B B B B B B B B B B B B B B B B B B B B B B B B <xsl:copy-of select="parent::section/title"/> B B B B B B B B B B B B B B B B B B B B </xsl:if> B B B B B B B B B B B B B B B B B B B B <xsl:copy> B B B B B B B B B B B B B B B B B B B B B B B B <xsl:copy-of select="title, intro"/> B B B B B B B B B B B B B B B B B B B B </xsl:copy> B B B B B B B B B B B B B B B B </xsl:element> B B B B B B B B B B B B </xsl:when> B B B B B B B B B B B B <!-- no children sections --> B B B B B B B B B B B B <xsl:when test=".[not(section)]"> B B B B B B B B B B B B B B B B <xsl:element name="file"> B B B B B B B B B B B B B B B B B B B B <xsl:if test="parent::section and position() = 1"> B B B B B B B B B B B B B B B B B B B B B B B B <xsl:copy-of select="parent::section/title"/> B B B B B B B B B B B B B B B B B B B B </xsl:if> B B B B B B B B B B B B B B B B B B B B <xsl:if test="position() = 1"> B B B B B B B B B B B B B B B B B B B B B B B B <xsl:copy-of select="parent::chapter/title"/> B B B B B B B B B B B B B B B B B B B B </xsl:if> B B B B B B B B B B B B B B B B B B B B <xsl:copy-of select="."/> B B B B B B B B B B B B B B B B </xsl:element> B B B B B B B B B B B B </xsl:when> B B B B B B B B B B B B <!-- recurse to get all levels --> B B B B B B B B B B B B <xsl:otherwise> B B B B B B B B B B B B B B B B <xsl:apply-templates select="section"/> B B B B B B B B B B B B </xsl:otherwise> B B B B B B B B </xsl:choose> B B B B </xsl:template> B B B B <!-- turn off title to output only when wanted --> B B B B <xsl:template match="title"/> </xsl:stylesheet> RFFFFFjoosfofopqcds'jnfs'output <?xml version="1.0" encoding="UTF-8"?> <wrapper> B B B B <file> B B B B B B B B <title>Chapter 1</title> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.1</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.2</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <title>Section 1.3</title> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.3.1</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.3.2</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.3.3</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.3.4</title> B B B B B B B B B B B B <intro/> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <title>Section 2.1</title> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 2.1.1</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 2.1.2</title> B B B B B B B B B B B B <intro/> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 2.2</title> B B B B B B B B </section> B B B B </file> </wrapper> <?xml version="1.0" encoding="UTF-8"?> <wrapper> B B B B <file> B B B B B B B B <title>Chapter 1</title> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.1</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.2</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <title>Section 1.3</title> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.3.1</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.3.2</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.3.3</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 1.3.4</title> B B B B B B B B B B B B <intro/> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <title>Section 2.1</title> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 2.1.1</title> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 2.1.2</title> B B B B B B B B B B B B <intro/> B B B B B B B B </section> B B B B </file> B B B B <file> B B B B B B B B <section> B B B B B B B B B B B B <title>Section 2.2</title> B B B B B B B B </section> B B B B </file> </wrapper> Terry On bThursdayb, bFebruaryb b01b, b2018b b03b:b05b:b49b bPM, Rick Quatro rick@xxxxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote: Hi, I am trying to solve a problem using XSLT 2. Here is my input: <?xml version="1.0" encoding="UTF-8"?> <book> B B <chapter> B B B B <title>Chapter 1</title> B B B B <section> B B B B B B <title>Section 1.1</title> B B B B </section> B B B B <section> B B B B B B <title>Section 1.2</title> B B B B </section> B B B B <section> B B B B B B <title>Section 1.3</title> B B B B B B <section> B B B B B B B B <title>Section 1.3.1</title> B B B B B B </section> B B B B B B <section> B B B B B B B B <title>Section 1.3.2</title> B B B B B B </section> B B B B B B <section> B B B B B B B B <title>Section 1.3.3</title> B B B B B B </section> B B B B B B <section> B B B B B B B B <title>Section 1.3.4</title> B B B B B B B B <intro/> B B B B B B B B <section> B B B B B B B B B B <title>Section 1.3.4.1</title> B B B B B B B B </section> B B B B B B B B <section> B B B B B B B B B B <title>Section 1.3.4.2</title> B B B B B B B B </section> B B B B B B B B <section> B B B B B B B B B B <title>Section 1.3.4.3</title> B B B B B B B B </section> B B B B B B </section> B B B B </section> B B </chapter> B B <chapter> B B B B <title>Chapter 2</title> B B B B <section> B B B B B B <title>Section 2.1</title> B B B B B B <section> B B B B B B B B <title>Section 2.1.1</title> B B B B B B </section> B B B B B B <section> B B B B B B B B <title>Section 2.1.2</title> B B B B B B B B <intro/> B B B B B B B B <section> B B B B B B B B B B <title>Section 2.1.2.1</title> B B B B B B B B </section> B B B B B B B B <section> B B B B B B B B B B <title>Section 2.1.2.2</title> B B B B B B B B </section> B B B B B B B B <section> B B B B B B B B B B <title>Section 2.1.2.3</title> B B B B B B B B </section> B B B B B B </section> B B B B </section> B B B B <section> B B B B B B <title>Section 2.2</title> B B B B </section> B B </chapter> </book> I want to split this content up into separate files (eventually HTML). Here is the desired output: <?xml version="1.0" encoding="UTF-8"?> <book> B B <file> B B B B <title>Chapter 1</title> B B B B <section> B B B B B B <title>Section 1.1</title> B B B B </section> B B </file> B B <file> B B B B <title>Section 1.2</title> B B </file> B B <file> B B B B <title>Section 1.3</title> B B B B <section> B B B B B B <title>Section 1.3.1</title> B B B B </section> B B </file> B B <file> B B B B <title>Section 1.3.2</title> B B </file> B B <file> B B B B <title>Section 1.3.3</title> B B </file> B B <file> B B B B <!-- Doesn't have to be with the following child section because it has <intro/> content. --> B B B B <title>Section 1.3.4</title> B B B B <intro/> B B </file> B B <file> B B B B <title>Section 1.3.4.1</title> B B </file> B B <file> B B B B <title>Section 1.3.4.2</title> B B </file> B B <file> B B B B <title>Section 1.3.4.3</title> B B </file> B B <file> B B B B <!-- All have to stay together because there is no content between the <title/> and child <section/> --> B B B B <title>Chapter 2</title> B B B B <section> B B B B B B <title>Section 2.1</title> B B B B B B <section> B B B B B B B B <title>Section 2.1.1</title> B B B B B B </section> B B B B </section> B B </file> B B <file> B B B B <title>Section 2.1.2</title> B B B B <intro/> B B </file> B B <file> B B B B <title>Section 2.1.2.1</title> B B </file> B B <file> B B B B <title>Section 2.1.2.2</title> B B </file> B B <file> B B B B <title>Section 2.1.2.3</title> B B </file> B B <file> B B B B <title>Section 2.2</title> B B </file> </book> The rules are as follows: A <section> can be in its own file if 1) There are no child <section> elements (like Section 1.2). 2) There is element content after the <title> and before the first child <section> element; for example an <intro> element (like Section 1.3.4). Otherwise, the <section> has to contain any first child descendants (like Chapter 2 through Section 2.1.1). This looks to me like a grouping problem where I can use <xsl:for-each-group>. I have this as a shell but I want to make sure I am on the right track. Thank you very much. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" B B xmlns:xs="http://www.w3.org/2001/XMLSchema" B B exclude-result-prefixes="xs" B B version="2.0"> B B B B <xsl:template match="/"> B B B B <xsl:for-each-group group-starting-with="//chapter|//section" select="."> B B B B B B <file> B B B B B B B B <xsl:copy-of select="current-group()"/> B B B B B B </file> B B B B </xsl:for-each-group> B B </xsl:template> </xsl:stylesheet> Rick
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Is split problem a grouping p, Rick Quatro rick@xxx | Thread | Re: [xsl] Is split problem a groupi, Rick Quatro rick@xxx |
[xsl] Is split problem a grouping p, Rick Quatro rick@xxx | Date | Re: [xsl] Is split problem a groupi, Rick Quatro rick@xxx |
Month |