Help with transforming xml
Posted: Mon May 07, 2007 6:16 am
Hi,
Newbie here (sorry). I'm opening Word docs in Open Office, which builds a nice little xml document called content.xml. One of the weird things that happens tho, is Open Office might use a paragraph or character style as is, or it MIGHT turn it into an automatic paragraph style with a parent-style-name attribute whose value is the original style name. Like so:
In the body of the document, there is either a reference to the actual stylename or a reference to the automatic stylename:
Also, there is no way to predict if and when Open Office will turn styles to automatic styles, nor can you count on the automatic-stylename to always be P1. The number is arbitrarily assigned. It could be P2, or P3, or Pn. So you can't reference it literally in the xslt.
What I'd like to do is write a transform so that if when the text:stylename (Pn) is the same as the automatic stylename, I can create an element based on the parent-style-name attribute, then put the contents of the text node inside that element. So here was (one of many) tries:
Unfortunately, all I get is the value of the first parent style name put into the element name:
I tried putting the xsl:for-each element around different tags, but that did not work. Any suggestions, pointers, or hints? I'd really appreciate it. Thanks in advance.
Greg
Newbie here (sorry). I'm opening Word docs in Open Office, which builds a nice little xml document called content.xml. One of the weird things that happens tho, is Open Office might use a paragraph or character style as is, or it MIGHT turn it into an automatic paragraph style with a parent-style-name attribute whose value is the original style name. Like so:
Code: Select all
<office:automatic-styles>
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="ChapterTitle"
style:master-page-name="Standard"></style:style>
...
</office:automatic-styles>
Code: Select all
<!--text:style-name should have been "ChapterTitle" but Open Office turned it into "P1"-->
<text:p text:style-name="P1">some text</text:p>
...
<!--sometimes Open Office leaves the style name alone-->
<text:p text:style-name="ChapterTitle">some text</text:p>
What I'd like to do is write a transform so that if when the text:stylename (Pn) is the same as the automatic stylename, I can create an element based on the parent-style-name attribute, then put the contents of the text node inside that element. So here was (one of many) tries:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0">
<xsl:output method="xml" indent="yes"></xsl:output>
<xsl:template match="/">
<xsl:apply-templates select="//text:p"></xsl:apply-templates>
</xsl:template>
<xsl:template match="text:p[@text:style-name=preceding::office:automatic-styles/style:style/@style:name]">
<xsl:element name="{preceding::office:automatic-styles/style:style/@style:parent-style-name}">
<xsl:value-of select=".">
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Code: Select all
<ChapterTitle>some text</ChapterTitle> <!--right--the parent style for P1>
<ChapterTitle>some other text</ChapterTitle> <!--wrong, should have been the parent style for P2-->
Greg