|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
XSL question
I have an XML with the following structure:
<styleguide> <rule> <name>style guide rule</name> <description>Some content <link href=”some url”>content that is a hyperlink </link> more content</description> </rule> <rule> <name>another style guide rule</name> <description>Content <link href="some url">content that is a hyperlink </link> More content </description> </rule> </styleguide> Not all description tags have content that are links but some do. Description tags may have 0 link tags or multiple link tags in them. My question is how do I display all the content within a description tag and have the link elements (if any exist) be displayed as hyperlinks? I can figure out how to display a description tag with one link element using xpath functions substring-before and substring-after but if there is more than one link element in the description tag I get stuck. My last question is, can variables be used in XSL like a standard programming language? If so I might be able to figure a solution with the use of variables. Any help on this would be gratefully appreciated. |
|
#2
|
|||
|
|||
|
You can give each link in your xml a unique id, eg
<styleguide> <rule> <name>style guide rule</name> <description>Some content <link id="1">content that is a hyperlink </link> more content</description> </rule> <rule> <name>another style guide rule</name> <description>Content <link id="2">content that is a hyperlink </link> More content </description> </rule> </styleguide> then add the following at the end of your file: <linkResources> <link id="1"> <linkURL>http://www.urlhere.com</linkURL> </link> <link id="2"> <linkURL>http://www.anotherurl.com</linkURL> </link> In the xsl file, you can then do the following: <xsl:template match="description"> <xsl:apply-templates/> </xsl:template> <xsl:template match="link | description/link"> <xsl:variable name="linkId"> <xsl:value-of select="@id"/> </xsl:variable> <xsl:variable name="linkURL"> <xsl:value-of select="/*/linkResources/link[@id=$linkId]/linkURL"/> </xsl:variable> <a href="{$linkURL}"><xsl:apply-templates/></a> </template> Where it says /*/ in the linkURL variable you may have to amend to what the specific path in your document is, this is just an example path. The two IDs are basically compared and if they match the value of linkURL is stored in the variable. This method will pick up all the text and links etc in the description tag. Also, you can keep all your page resources like links in a central place in your document. Hope this helps ![]() |
|
#3
|
|||
|
|||
|
Thanks a ton for the reply. I applyed the style sheet and with a little bit of tweaking got it to work.
|
|
#4
|
|||
|
|||
|
If that way works, great, but here's a way that's a bit less complicated and lets your retain your earlier (simpler) xml structure.
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="//rule">
<p>
<xsl:apply-templates />
</p>
</xsl:template>
<xsl:template match="//name">
<b><xsl:apply-templates /></b><br/>
</xsl:template>
<xsl:template match="//description">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="//link">
<a href="{@href}"><xsl:apply-templates /></a>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
|
|
#5
|
|||
|
|||
|
Thanks for the help. I applied the second style sheet and it works great and keeps things simple. Makes my boss happy.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > XSL question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|