Jump to content

Interchange elements


PPRO_47

Recommended Posts

Hi Everyone!I'm new with this kind of language, I'm trying to switch the position of "author" and "year" but I'm not getting the result I wanted. The content of author and year is the same with every book, what I need is the content of the author and year of a certain book. And also can you please check if there's a better way interchanging the 2 elements.This is the sample xml:

<bookstore>    <book category="COOKING">        <title lang="en">Everyday Italian</title>        <author>Giada De Laurentiis</author>        <year>2005</year>        <price>30.00</price>    </book>    <book category="CHILDREN">        <title lang="en">Harry Potter</title>        <author>J K. Rowling</author>        <year>2005</year>        <price>29.99</price>    </book>    <book category="WEB">        <title lang="en">XQuery Kick Start</title>        <author>James McGovern</author>        <author>Per Bothner</author>        <author>Kurt Cagle</author>        <author>James Linn</author>        <author>Vaidyanathan Nagarajan</author>        <year>2003</year>        <price>49.99</price>    </book>    <book category="WEB">        <title lang="en">Learning XML</title>        <author>Erik T. Ray</author>        <year>2003</year>        <price>39.95</price>    </book></bookstore>

This is my Xsl code:

<xsl:template match="title">		<xsl:element name="{name(.)}">			<xsl:attribute name="aid:pstyle">H1</xsl:attribute>			<xsl:apply-templates/>		</xsl:element>		</xsl:template>	<xsl:template match="author">		<xsl:element name="year">			<xsl:attribute name="aid:pstyle">Y1</xsl:attribute>			<xsl:value-of select="//year"/>		</xsl:element>	</xsl:template>	<xsl:template match="year">		<xsl:element name="author">			<xsl:attribute name="aid:pstyle">A1</xsl:attribute>			<xsl:value-of select="//author"/>		</xsl:element>	</xsl:template>   	<xsl:template match="price"/>

This is the result:

<title xmlns:aid="Indesign/support" aid:pstyle="H1">Everyday Italian</title>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">Giada De Laurentiis</author>			<title xmlns:aid="Indesign/support" aid:pstyle="H1">Harry Potter</title>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">Giada De Laurentiis</author>			<title xmlns:aid="Indesign/support" aid:pstyle="H1">XQuery Kick Start</title>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">Giada De Laurentiis</author>			<title xmlns:aid="Indesign/support" aid:pstyle="H1">Learning XML</title>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">Giada De Laurentiis</author>

Can someone help me! Thanks!

Link to comment
Share on other sites

You might want to add a template that will explicitly copy anything non matched. Something like:

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

or perhaps:

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

I'm not completely sure what's the result you'd like to get (I see what you're getting, but how does it differ from your final goal?), but nevertheless, this should be part of the solution.

Link to comment
Share on other sites

Thanks for your reply, the result I wanted is like this:

<title xmlns:aid="Indesign/support" aid:pstyle="H1">Everyday Italian</title>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">Giada De Laurentiis</author>			<title xmlns:aid="Indesign/support" aid:pstyle="H1">Harry Potter</title>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2005</year>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">J K. Rowling</author>		<title xmlns:aid="Indesign/support" aid:pstyle="H1">XQuery Kick Start</title>		<year xmlns:aid="Indesign/support" aid:pstyle="Y1">2003</year>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">James McGovern</author>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">Per Bothner</author>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">Kurt Cagle</author>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">James Linn</author>		<author xmlns:aid="Indesign/support" aid:pstyle="A1">Vaidyanathan Nagarajan</author>...

I just want to move the author to year's place and vice versa.

Link to comment
Share on other sites

Hi! here it is.

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aid="Indesign/support"	version="1.0">	<xsl:template match="*|@*">		<xsl:copy>			<xsl:apply-templates/>		</xsl:copy>	</xsl:template>		<xsl:template match="title">		<xsl:element name="{name(.)}">			<xsl:attribute name="aid:pstyle">H1</xsl:attribute>			<xsl:apply-templates/>		</xsl:element>		</xsl:template>	<xsl:template match="author">		<xsl:element name="year">			<xsl:attribute name="aid:pstyle">Y1</xsl:attribute>			<xsl:value-of select="//year"/>		</xsl:element>	</xsl:template>	<xsl:template match="year">		<xsl:element name="author">			<xsl:attribute name="aid:pstyle">A1</xsl:attribute>			<xsl:value-of select="//author"/>		</xsl:element>	</xsl:template>   	<xsl:template match="price"/></xsl:stylesheet>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...