|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Using XSLT to set attribute value.
Hello everybody,
Let me present the problem. Let's say I have an XML file like this: <root> <link source="www.google.com">Google</link> </root> And I want to tranform it into this using XSLT: <a href="www.google.com">Google</a> This is what I have so far for an XSLT: <xsl:template match="/"> <a href = " [unknown] "> <xsl:value-of select="root/link" /> </a> </xsl:template> I need to figure out how to put the source attribute from the link element into the href attribute (the unknown). Any help would be appreciated. |
|
#2
|
|||
|
|||
|
Quote:
Hello. Try: <xsl:template match="/"> ... <xsl:apply-templates select="/root/link" /> ... </xsl:template <xsl:template match="link"> <a href="{@source}"><xsl:value-of select="." /> </xsl:template> You can apply the link template individually or you can apply ALL defined templates by simply stating: <xsl:apply-templates /> -- be careful with this, though, as it can lead to duplicate information if you reference other templates individually. Secondly, in order to specify an Xpath expression to assign an attribute to an HTML element, you need to escape it with {} within the quotes, hence href="{@source}". You can specify any XPath expression (to my knowledge) within the curly braces -- so something like href="{link/external/href}" could be used if you had an xml structure like: <link> <external> <text>Text</text> <href>www.google.com</href> </extrenal> </link> Hope that helps. |
|
#3
|
||||
|
||||
|
Here's another way to skin that cat as well:
PHP Code:
PHP Code:
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Using XSLT to set attribute value. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|