Ich habe eine Haupt-Xslt, die cdcatalog.xsl
ist und mit Xml-Datei cdcatalog.xml
verknüpft.
Ich versuche, Vorlagen dynamisch basierend auf dem Wert in country
anzuwenden. Einige der tatsächlichen Vorlagen sind externe .xsl
Dateien.XSL-Vorlage dynamisch anwenden. Vorlage ist in externen Xslt-Datei und in Main Xslt
cdcatalog.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:include href="cdcatalog_in.xsl"/>
<xsl:include href="cdcatalog_usa.xsl"/>
<xsl:include href="cdcatalog_uk.xsl"/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
cdcatalog.xml Inhalte sind wie folgt:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>IN</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
</catalog>
cdcatalog_in.xsl Inhalte sind wie folgt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="cd[country='IN']" name="IN" >
<h4>Country: India</h4>
</xsl:template>
</xsl:stylesheet>
cdcatalog_usa.xsl Inhalte sind wie folgt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="cd[country='USA']" name="USA" >
<h4>Country: United States of America</h4>
</xsl:template>
</xsl:stylesheet>
cdcatalog_uk.xsl Inhalte sind wie folgt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="cd[country='UK']" name="UK" >
<h4>Country: United Kingdom</h4>
</xsl:template>
</xsl:stylesheet>
Erwartete Ausgabe
Title : Empire Burlesque
Artist: Bob Dylan
Country: United States of America
Title : Hide your heart
Artist: Bonnie Tyler
Country: United Kingdom
Title : Greatest Hits
Artist: Dolly Parton
Country: United States of America
Title : Still got the blues
Artist: Gary Moore
Country: India
Ge tting unter Ausgabe, die
My CD Collection
Country: United States of America 2
Country: United Kingdom
Country: United States of America 2
Country: India
Vielen Dank im Voraus nicht korrekt ist. Srini
Gut zu handhaben Unbekannter Fall – Rao
Anstatt "UNKNOWN" zu zeigen, wenn die entsprechende Vorlage fehlt, wird wahrscheinlich der gleiche Ländercode angezeigt? – Rao
diese Lösung funktioniert gut. Außerdem befinden wir uns noch in der Entwurfsphase, sodass wir Änderungen an externen Vorlagen vornehmen können. – SriniRao