XSL formatting issue

Here should go questions about transforming XML with XSLT and FOP.
sxxx
Posts: 6
Joined: Sun Mar 27, 2005 10:29 pm

XSL formatting issue

Post by sxxx »

Hi all,

I have an xml document which is as follows

Code: Select all


<plist>
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDisplayName</key>
<string>TextEdit</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>rtf</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>rtf.icns</string>
<key>CFBundleTypeMIMETypes</key>
<array>
<string>text/rtf</string>
</array>
<key>CFBundleTypeName</key>
<string>NSRTFPboardType</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>RTF </string>
</array>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSIsAppleDefaultForType</key>
<true/>
</dict>
</dict>
</plist>

i want the output to look like this

"CFBundleDevelopmentRegion" = "English";
"CFBundleDisplayName" = "TextEdit";
"CFBundleDocumentTypes" = (
{
"CFBundleTypeExtensions" = (
"rtf"
);
"CFBundleTypeIconFile" = "rtf.icns";
"CFBundleTypeMIMETypes" = (
"text/rtf"
);
and so on.

does anyone have any ideas???
sxxx
Posts: 6
Joined: Sun Mar 27, 2005 10:29 pm

Re: XSL formatting issue

Post by sxxx »

sxxx wrote:Hi all,

I have an xml document which is as follows

Code: Select all


<plist>
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDisplayName</key>
<string>TextEdit</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>rtf</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>rtf.icns</string>
<key>CFBundleTypeMIMETypes</key>
<array>
<string>text/rtf</string>
</array>
<key>CFBundleTypeName</key>
<string>NSRTFPboardType</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>RTF </string>
</array>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSIsAppleDefaultForType</key>
<true/>
</dict>
</dict>
</plist>

i want the output to look like this

"CFBundleDevelopmentRegion" = "English";
"CFBundleDisplayName" = "TextEdit";
"CFBundleDocumentTypes" = (
{
"CFBundleTypeExtensions" = (
"rtf"
);
"CFBundleTypeIconFile" = "rtf.icns";
"CFBundleTypeMIMETypes" = (
"text/rtf"
);
and so on.
i need to have a xsl document to do this formatting
does anyone have any ideas???
xacobea
Posts: 5
Joined: Wed Jan 05, 2005 1:07 pm

Post by xacobea »

i think it would be easier if you change your xml adding an 'item' tag:

Code: Select all


<dict>
<item>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
</item>
</dict>
i have this output:

Code: Select all


"CFBundleDevelopmentRegion" = "English"
"CFBundleDisplayName" = "English"
"CFBundleDocumentTypes" = "English"

{
"CFBundleTypeExtensions" = (
"rtf"
);
"CFBundleTypeIconFile" = (
"rtf"
);
"CFBundleTypeMIMETypes" = (
"rtf"
);
"CFBundleTypeName" = (
"rtf"
);
"CFBundleTypeOSTypes" = (
"rtf"
);
"CFBundleTypeRole" = (
"rtf"
);
"LSIsAppleDefaultForType" = (
"rtf"
);
}
created with this xslt (i know about the missing line breaks, but that's the only way i get the resulting output with 'xsltproc'):

Code: Select all


  <xsl:output method="text"/>

<xsl:template match="/plist">

<xsl:apply-templates select="dict/key" />
{<xsl:text>
</xsl:text>&#160;&#160;<xsl:apply-templates select="dict/array/dict/key" mode="second"/>}

</xsl:template>

<xsl:template match="key">"<xsl:value-of select="." />" = "<xsl:value-of select="../string" />"<xsl:text>
</xsl:text></xsl:template>

<xsl:template match="key" mode="second">"<xsl:value-of select="." />" = (<xsl:text>
</xsl:text>&#160;&#160;"<xsl:value-of select="../array/string" />"<xsl:text>
</xsl:text>);<xsl:text>
</xsl:text></xsl:template>
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

The following stylesheet should generate what you want including the indents.

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>

<xsl:template name="doIndent">
<xsl:param name="indent" select="0"/>
<xsl:if test="$indent>0">
<xsl:text> </xsl:text>
<xsl:call-template name="doIndent">
<xsl:with-param name="indent" select="$indent - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>


<xsl:template match="plist">
<xsl:apply-templates>
<xsl:with-param name="indent" select="0"/>
</xsl:apply-templates>
</xsl:template>


<xsl:template match="dict">
<xsl:param name="indent" select="0"/>
<xsl:call-template name="doIndent">
<xsl:with-param name="indent" select="$indent"/>
</xsl:call-template>
<xsl:text>{
</xsl:text>
<xsl:apply-templates select="key">
<xsl:with-param name="indent" select="$indent+1"/>
</xsl:apply-templates>
<xsl:call-template name="doIndent">
<xsl:with-param name="indent" select="$indent"/>
</xsl:call-template>
<xsl:text>}
</xsl:text>
</xsl:template>

<xsl:template match="key">
<xsl:param name="indent" select="0"/>
<xsl:call-template name="doIndent">
<xsl:with-param name="indent" select="$indent"/>
</xsl:call-template>
<xsl:text>"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>" = </xsl:text>
<xsl:apply-templates select="following-sibling::*[1]">
<xsl:with-param name="indent" select="$indent"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="array">
<xsl:param name="indent" select="0"/>
<xsl:text>(
</xsl:text>
<xsl:for-each select="*">
<xsl:call-template name="doIndent">
<xsl:with-param name="indent" select="$indent+1"/>
</xsl:call-template>

<xsl:apply-templates select=".">
<xsl:with-param name="indent" select="$indent+1"/>
</xsl:apply-templates>
</xsl:for-each>
<xsl:call-template name="doIndent">
<xsl:with-param name="indent" select="$indent"/>
</xsl:call-template>
<xsl:text>)
</xsl:text>
</xsl:template>


<xsl:template match="string">
<xsl:text>"</xsl:text>
<xsl:value-of select="."/>
<xsl:text>"
</xsl:text>
</xsl:template>


<xsl:template match="true">
<xsl:text>"true"
</xsl:text>
</xsl:template>


<xsl:template match="false">
<xsl:text>"false"
</xsl:text>
</xsl:template>

<xsl:template match="*|text()"/>
</xsl:stylesheet>
Best Regards,
George
Post Reply