У меня есть один основной xslt, который является cdcatalog.xsl
и связан с файлом xml cdcatalog.xml
.
Я пытаюсь применить шаблоны динамически на основе значения в country
. Некоторые из фактических шаблонов являются внешними .xsl
файлами.применять шаблон XSL динамически. Шаблон находится во внешнем файле xslt и включен в основной 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 содержимое, как показано ниже:
<?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 содержание, как показано ниже:
<?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 содержание, как показано ниже:
<?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 содержание, как показано ниже:
<?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>
Ожидаемые результаты
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 чал ниже выход, который является неправильным
My CD Collection
Country: United States of America 2
Country: United Kingdom
Country: United States of America 2
Country: India
Заранее спасибо. Srini
Хорошо обрабатывать неизвестный случай страны – Rao
Вместо того, чтобы показывать 'UNKNOWN', когда отсутствует соответствующий шаблон, возможно, может отображаться один и тот же код страны? – Rao
Это решение работает нормально. Также мы все еще находимся в стадии разработки, поэтому мы можем вносить изменения в внешние шаблоны. – SriniRao