Jump to content

XLST questions for repurposing magazine to other platforms


yoshimura

Recommended Posts

Hello,I'm trying to figure out how to transform an exported XML file from InDesign.1) How can I transform every instance of a tag (like <Body_without_indent>) into a new tag (<Body.1>)?2) Can I select an xml element and use it more than once? I would like to take stories from within the magazine that are attributed with ordinal numbers and have them as snippets on the front page (1,2,3…). The whole of the stories need to be within the magazine as well.Example of code:

<Magazine>	<On_scene>		<Dom_head>text</Dom_head>		<Heading>text</Heading>		<Image></Image>		<Caption>Text<_C_Photo_credit_>(Photo: Credit)</_C_Photo_credit_>Text</Caption>		<Byline>text</Byline>		<email>text@text.com<email>		<SummaryDeck>text</SummaryDeck>		<Body_without_indent>text</Body_without_indent>		<Body_with_indent>text</Body_with_indent>		<Body_with_indent>text</Body_with_indent>		<Body_with_indent>text</Body_with_indent>		<Body_with_indent>text;</Body_with_indent>		<Jump_line_suite>text</Jump_line_suite>		<Image></Image>		<Caption>text<_C_Photo_credit_>(Photo: Credit)</_C_Photo_credit_>Text</Caption>		<Jump_line_suite>text</Jump_line_suite>		<Body_without_indent>text</Body_without_indent>		<Body_with_indent>text</Body_with_indent>		<Subheading>text</Subheading>		<Body_without_indent>text</Body_without_indent>		<Body_with_indent>text</Body_with_indent>		<Body_with_indent>text</Body_with_indent>		<Body_with_indent>text</Body_with_indent>	</On_scene>	…

Cheers

Link to comment
Share on other sites

If you want to copy most of your XML but change/add/delete some nodes you usually start with the following template, called the identity transformation

<xsl:template match="@* | node()">  <xsl:copy>	<xsl:apply-templates select="@* | node()"/>  </xsl:copy></xsl:template>

Then you can add more templates for the changes you want to achieve e.g.

<xsl:template match="Body_without_indent">  <Body.1>	<xsl:apply-templates select="@* | node()"/>  </Body.1></xsl:template>

As for processing nodes more than once, that is certainly possible, the cleanest solution is usually to use a separate mode (i.e. by putting a mode attribute on xsl:template and xsl:apply-templates).

Link to comment
Share on other sites

Thanks for the reply Martin,The identity transformation worked! :) Can I use this methodology to transform the XML into HTML?For example,The <On_scene> article needs to be displayed as HTML.Every instance of <Body_with_indent> and <Body_without_indent> need to appear in their proper order and as paragraphs <p>.I was guessing it would be something like this, but I don't know where to place the <head> and <body> tags:

<xsl:template match="Body_without_indent">  <p>	<xsl:apply-templates select="@* | node()"/>  </p></xsl:template>

Link to comment
Share on other sites

Add a template
<xsl:template match="/">  <html>	<head>	  <title>...</title>   </head>   <body>	  <xsl:apply-templates/>   </body></xsl:template>

then you can use that approach. And yes, your template transforming a Body_without_indent into a HTML p element is fine.

Amazing! :)It works like a charm.A question about the "mode" attribute:Like we discussed, I'd like to take stories that are tagged and attributed in order of importance ("One", "Two", "Three"…etc.) and copy them to the top of the XML tree; to be used for the front page of the magazine. So, duplicate the nodes that have the attributes. The duplicates have to be placed as the first children of the root.In my attempt below, I tried to ask the XSLT to duplicate any descendant node of the root attributed with One:
<xsl:template match="@* | node()">		 <xsl:copy>			 <xsl:apply-templates select="descendant" mode="One"></xsl:apply-templates>			 <xsl:apply-templates select="@* | node()"/>		 </xsl:copy>	 </xsl:template>	 <xsl:template match="Byline_email">	 </xsl:template>	 <xsl:template match="Body_without_indent">		 <Body.1>			 <xsl:apply-templates select="@* | node()"/>		 </Body.1>	 </xsl:template>

Link to comment
Share on other sites

You might want to show us minimal but complete samples of XML input and desired XML output so that we can understand what you want to achieve. I am currently not sure what kind of result you want to achieve and whether you really need to use modes, maybe doing xsl:copy-of select="someElement" at the right place suffices to copy what you want to copy.As for you latest code snippet select="descendant"would simply select child elements named "descendant". If you want to select all descendant elements you need select="descendant::*"And putting a mode="One" on an apply-templates only makes sense if you also have templates with that mode attribute.

Link to comment
Share on other sites

You might want to show us minimal but complete samples of XML input and desired XML output so that we can understand what you want to achieve. I am currently not sure what kind of result you want to achieve and whether you really need to use modes, maybe doing xsl:copy-of select="someElement" at the right place suffices to copy what you want to copy.As for you latest code snippet select="descendant"would simply select child elements named "descendant". If you want to select all descendant elements you need select="descendant::*"And putting a mode="One" on an apply-templates only makes sense if you also have templates with that mode attribute.
The best way for me to explain is to show the XML structure via InDesign.There is a print edition of the magazine and I need to repurpose it for the iPad. I would like copy elements from articles that are tagged and so that they can be sent to the front page of the iPad edition.Here is an overview on the XML structure. The root is Magazine and the children are articles for various sections of the magazine.4954617876_4eb0dc0e15.jpgThe subheading(Subhead) and summary(SummaryDeck) of the Sur_scene article are attributed with "Un". Which means that they are to be the main feature on the front page of the iPad edition.4954617880_2f946786e8.jpgThe heading and summary of the Exposition article are attributed with "Deux". This is meant to be the second most important article on the front page.4954617884_e06a80b159.jpgThe heading and summary of the Toronto article are attributed with "Trois". It will be the third most important article to appear on the front page.4954617888_69af0bb44c.jpgThe result should look like this:4954617874_a70f100d82.jpgThe attributed elements should be copied and moved to the top of the structure.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...