Pergunta

I have the following part of an XML (It's ODF)

<office:body>
    <office:text text:use-soft-page-breaks="true">
        <text:h text:style-name="P1" text:outline-level="1">Heading 1</text:h>
        <text:p text:style-name="P2">Paragraph 1</text:p>
        <text:h text:style-name="P3" text:outline-level="2">Heading 2</text:h>
        <text:p text:style-name="P4">Paragraph 2</text:p>
        <text:p text:style-name="P5">Paragraph 3</text:p>
        <text:h text:style-name="P6" text:outline-level="3">Heading 3</text:h>
        <text:p text:style-name="P7">Paragraph 4</text:p>
        <text:p text:style-name="P8">Paragraph 5</text:p>
        <text:p text:style-name="P9">Paragraph 6</text:p>
        <text:h text:style-name="P10" text:outline-level="4">Heading 4</text:h>
        <text:p text:style-name="P11">Paragraph 7</text:p>
        <text:h text:style-name="P12" text:outline-level="2">Heading 2</text:h>
        <text:p text:style-name="P13">Paragraph 8</text:p>
        <text:p text:style-name="P14">Paragraph 9</text:p>
        <text:p text:style-name="Normal">
            <text:span text:style-name="T15">Paragraph 10</text:span>
        </text:p>
    </office:text>
</office:body>

I need to transform this to

<Blocks>
    <Block>
        <Title><![CDATA[Heading 2]]></Title>
        <Content>
            <![CDATA[<p>Paragraph 2</p><p>Paragraph 3</p><h3>Heading 3</h3><p>Paragraph 4</p><p>Paragraph 5</p><p>Paragraph 6</p><h4>Heading 4</h4><p>Paragraph 7</p>]]>
        </Content>
    </Block>
    <Block>
        <Title><![CDATA[Heading 2]]></Title>
        <Content>
            <![CDATA[<p>Paragraph 8</p><p>Paragraph 9</p><p>Paragraph 10</p>]]>
        </Content>
    </Block>
</Blocks>

As you can see, I want to create a Block element for each text:h/@text:outline-level = 2 node.

All following text:p and text:h/@text:outline-level > 2 siblings should be placed in a Content element inside the just created Block element.

How can I achieve that?

Foi útil?

Solução

Here is a partial solution:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:office="http://example.com/office"
  xmlns:text="http://example.com/text"
  exclude-result-prefixes="office text">

<xsl:output indent="yes" cdata-section-elements="Title"/>

<xsl:key 
  name="k1"
  match="text:p"
  use="generate-id(preceding-sibling::text:h[@text:outline-level = 2][1])"/>

<xsl:key 
  name="k1"
  match="text:h[@text:outline-level > 2]"
  use="generate-id(preceding-sibling::text:h[@text:outline-level = 2])"/>

<xsl:template match="office:text">
  <Blocks>
    <xsl:apply-templates select="text:h[@text:outline-level = 2]"/>
  </Blocks>
</xsl:template>

<xsl:template match="text:h[@text:outline-level = 2]">
  <Block>
    <Title>
      <xsl:value-of select="."/>
    </Title>
    <Content>
      <xsl:apply-templates select="key('k1', generate-id())"/>
    </Content>
  </Block>
</xsl:template>

<xsl:template match="text:p">
  <p>
    <xsl:apply-templates/>
  </p>
</xsl:template>

<xsl:template match="text:h[@text:outline-level = 3]">
  <h3>
    <xsl:apply-templates/>
  </h3>
</xsl:template>

<xsl:template match="text:h[@text:outline-level = 4]">
  <h4>
    <xsl:apply-templates/>
  </h4>
</xsl:template> 

</xsl:stylesheet>

It transforms

<office:body xmlns:office="http://example.com/office" xmlns:text="http://example.com/text">
    <office:text text:use-soft-page-breaks="true">
        <text:h text:style-name="P1" text:outline-level="1">Heading 1</text:h>
        <text:p text:style-name="P2">Paragraph 1</text:p>
        <text:h text:style-name="P3" text:outline-level="2">Heading 2</text:h>
        <text:p text:style-name="P4">Paragraph 2</text:p>
        <text:p text:style-name="P5">Paragraph 3</text:p>
        <text:h text:style-name="P6" text:outline-level="3">Heading 3</text:h>
        <text:p text:style-name="P7">Paragraph 4</text:p>
        <text:p text:style-name="P8">Paragraph 5</text:p>
        <text:p text:style-name="P9">Paragraph 6</text:p>
        <text:h text:style-name="P10" text:outline-level="4">Heading 4</text:h>
        <text:p text:style-name="P11">Paragraph 7</text:p>
        <text:h text:style-name="P12" text:outline-level="2">Heading 2</text:h>
        <text:p text:style-name="P13">Paragraph 8</text:p>
        <text:p text:style-name="P14">Paragraph 9</text:p>
        <text:p text:style-name="Normal">
            <text:span text:style-name="T15">Paragraph</text:span>
        </text:p>
    </office:text>
</office:body>

into

<Blocks>
   <Block>
      <Title><![CDATA[Heading 2]]></Title>
      <Content>
         <p>Paragraph 2</p>
         <p>Paragraph 3</p>
         <h3>Heading 3</h3>
         <p>Paragraph 4</p>
         <p>Paragraph 5</p>
         <p>Paragraph 6</p>
         <h4>Heading 4</h4>
         <p>Paragraph 7</p>
      </Content>
   </Block>
   <Block>
      <Title><![CDATA[Heading 2]]></Title>
      <Content>
         <p>Paragraph 8</p>
         <p>Paragraph 9</p>
         <p>
            Paragraph
        </p>
      </Content>
   </Block>
</Blocks>

So the grouping is done as far as I can tell, although the last p in the second Block does not appear in your required output but as your verbal description "All following text:p" does not suggest otherwise I have grouped following that description.

What is furthermore lacking is stuffing all those p and hx elements into a CDATA section, I am not sure you really want that, if so, I would suggest to grab something like http://lenzconsulting.com/xml-to-string/ or to use an extension function to serialize nodes to text. That might also need use of result tree fragments and use of exsl:node-set or similar to convert result tree fragments to node-sets for serializing. As an alternative you would need to change the templates for text:p and others to output text nodes with markup instead of element nodes.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top