Jump to content

Xsl For Selecting Elements With Attributes


Brig

Recommended Posts

Hello,I need to display parts of an XML file as HTML.I can easily do this with well formed standard XML but this XML file I've been given really confuses me.All the elements have the same name but instead have unique attributes.Could anyone point me in the right direction for this please?I need the XSL code to select only these elements from the XML file:

<var name='HEADLINE'>	<string>Neoteny: how leaders recruit the right kind of followers</string></var><var name='SUMMARY'>	<string>Vissa ledare lyckas bättre än andra att rekrytera sina efterträdare - vad karaktäriserar dem?</string></var><var name='BYLINE'>	<string>Xxxxx Xxxxxxxx</string></var><var name='ATTACHMENT'>	<string>HBR%20%2D%20How%20leaders%20recruit%20their%20followers%20030210.pdf</string></var>

Here is the source XML file:

<wddxPacket version='1.0'>	<header/>	<data>		<struct>			<var name='DOCNO'>				<string></string>			</var>			<var name='KEYWORDS'>				<string></string>			</var>			<var name='ARTICLEID'>				<string>15</string>			</var>			<var name='TOPICIDS'>				<string>11,1026,1029,1002,1008,1013</string>			</var>			<var name='COPYRIGHTLINE'>				<string></string>			</var>			<var name='MODIFIEDBY_NAME'>				<string>Xxxxx Xxxxxxx</string>			</var>			<var name='AUTHORMAIL'>				<string>xxxx.xxxx@xxx.com</string>			</var>			<var name='INFOCODES'>				<string>Programtyp|Acrobat,Dokumenttyp|Artikel,Sekretessniv&aring;|&Ouml;ppen spridning</string>			</var>			<var name='SUMMARY'>				<string>Vissa ledare lyckas bättre än andra att rekrytera sina efterträdare - vad karaktäriserar dem?</string>			</var>			<var name='AUTHORID'>				<string>6</string>			</var>			<var name='HEADLINE'>				<string>Neoteny: how leaders recruit the right kind of followers</string>			</var>			<var name='ATTACHMENT'>				<string>HBR%20%2D%20How%20leaders%20recruit%20their%20followers%20030210.pdf</string>			</var>			<var name='BYLINE'>				<string>Xxxxx Xxxxxxxx</string>			</var>			<var name='DATELINE'>				<string>2002-06-01</string>			</var>			<var name='DATEEXPIRE'>				<string>2005-06-12</string>			</var>			<var name='INFOCODEIDS'>				<string>8,13,2</string>			</var>			<var name='DATEENTERED'>				<string>2003-06-13</string>			</var>			<var name='MODIFIEDBY_ID'>				<string>6</string>			</var>			<var name='ATTACHMENTURL'>				<string></string>			</var>			<var name='ORIGINALAUTHOR'>				<string></string>			</var>			<var name='ORIGINALARTID'>				<string></string>			</var>			<var name='DOCREV'>				<string></string>			</var>			<var name='MODIFIED_DATE'>				<string>2003-06-13</string>			</var>			<var name='USERGROUPID'>				<string>1</string>			</var>			<var name='PROVIDER'>				<string>Xxxxxx</string>			</var>			<var name='SOURCEMETADATA'>				<string></string>			</var>			<var name='SOURCE'>				<string>Harvard Management Update</string>			</var>			<var name='ARTICLETYPE'>				<string>1</string>			</var>			<var name='TOPIC'>				<string>Tjänsteområde|HR Strategy & Organization,Ämne|Rekrytera,Ämne|Ledarskap</string>			</var>			<var name='NEWSLINETEXT'>				<string></string>			</var>		</struct>	</data></wddxPacket>

Link to comment
Share on other sites

Are you going to process all of those in the same fashion, or do you have something special in store for each?If all are to be in the same fashion, I'd reccomend something like:

<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><head><!--Other HTML stuff--></head><body><!--Other HTML stuff--><xsl:apply-tempaltes/></body></html></xsl:template><xsl:template match="var[@name='HEADLINE' or @name='SUMMARY' or @name='BYLINE' or @name='ATTACHMENT'"><xsl:value-of select="string"/><!--Replace with whatever you want to do--></xsl:template></xsl:stylesheet>

If each must be processed in a different fashion, it's almost the same deal, but with several templates instead of one template like the second... like:

<xsl:template match="var[@name='HEADLINE'"><xsl:value-of select="string"/><!--Replace with whatever you want to do--></xsl:template><xsl:template match="var[@name='SUMMARY'"><xsl:value-of select="string"/><!--Replace with whatever you want to do--></xsl:template><!--etc.-->

Link to comment
Share on other sites

Thanks boen_robot. All of them will be in the same way but on seperate lines.As in:

<p>Neoteny: how leaders recruit the right kind of followers</p><h1>Vissa ledare lyckas bättre än andra att rekrytera sina efterträdare - vad karaktäriserar dem?</h1><p>Xxxxx Xxxxxxxx</p><p>HBR%20%2D%20How%20leaders%20recruit%20their%20followers%20030210.pdf</p>

I've tried your code in the first and second example but it brings all elements anyway not just the ones specified in the template match tag.Maybe I'm missing something?Here's the code I'm trying, I'm working in Dreamweaver CS4.

<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="http://www.domain.com/joh/page.xml" --><!DOCTYPE xsl:stylesheet  [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg	"®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen	"¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/><xsl:template match="/"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>Untitled Document</title></head><body><xsl:apply-templates/></body></html></xsl:template><xsl:template match="var[@name='HEADLINE']"><xsl:value-of select="string"/><!--Replace with whatever you want to do--></xsl:template><xsl:template match="var[@name='SUMMARY']"><xsl:value-of select="string"/><!--Replace with whatever you want to do--></xsl:template><xsl:template match="var[@name='BYLINE']"><xsl:value-of select="string"/><!--Replace with whatever you want to do--></xsl:template><xsl:template match="var[@name='ATTACHMENT']"><xsl:value-of select="string"/><!--Replace with whatever you want to do--></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Opps... my fault... if all you want is those "var" elements, replace

<body><xsl:apply-templates/></body>

with

<body><xsl:apply-templates select="//var"/></body>

If that too doesn't work, try to add this template to the stylesheet:

<xsl:template match="var" />

(which basically says "don't do anything for all var elements"; the other templates are more specific, so they should be of higher priority, and therefore execute)

Link to comment
Share on other sites

I guess this is what you want:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:longrun="http://longrun" version="2.0"> <xsl:output method="xml" encoding="UTF-8" indent="yes" /> <xsl:template match="/"> <xsl:apply-templates select="//var" mode="frontControllerTemplate" /> </xsl:template> <xsl:template match="var" mode="frontControllerTemplate"> <xsl:choose> <xsl:when test="@name = 'TOPICIDS' or @name = 'SUMMARY'"> <xsl:apply-templates select="." mode="include" /> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="." mode="exclude" /> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="var" mode="include"> <xsl:element name="var"> <xsl:attribute name="name"> <xsl:value-of select="@name"/> </xsl:attribute> <xsl:element name="string"> <xsl:value-of select="string" /> </xsl:element> </xsl:element> </xsl:template> <xsl:template match="var" mode="exclude"> <!-- leave empty --> </xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...