[英]Get value of child nodes in XSLT
我對XML和XSLT並不陌生,但是我試圖創建一個HTML頁面,該頁面循環經過不同的步驟,並生成HTML列表。
XML看起來像這樣:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<module>
<name ID="SDCModule002">
<role>SDC</role>
<modNum>002</modNum>
<title>Test</title>
<audience>me</audience>
<numSteps>7</numSteps>
<steps>
<step1>Do this 1</step1>
<step2>Do this 2</step2>
<step3>Do this 3</step3>
<step4>Do this 4</step4>
<step5>Do this 5</step5>
<step6>Do this 6</step6>
<step7>Do this 7</step7>
</steps>
</name>
<name ID="SDCModule003">
<role>SDC</role>
<modNum>003</modNum>
<title>Hi</title>
<audience>you</audience>
<numSteps>4</numSteps>
<steps>
<step1>Hi</step1>
<step2>There</step2>
<step3>You</step3>
<step4>Guys</step4>
</steps>
</name>
</module>
</xml>
我有它,所以要顯示我正在搜索的模塊的標題,受眾,任務等信息,以便可以使用。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/xml">
<html>
<head>
<title><xsl:value-of select="module/name[@ID='SDCModule001']/title "/></title>
</head>
<xsl:apply-templates select="module/name[@ID='SDCModule001']"/>
</body>
</html>
</xsl:template>
<xsl:template name="stepList" match="name">
<div id= "info">
<center>
<font face="verdana" size="2"><b><center><xsl:value-of select="title" /></center></b></font>
<hr></hr>
<xsl:value-of select="audience" />
<p></p>
<xsl:value-of select="numSteps" />
</center>
</div>
</xsl:template>
</xsl:stylesheet>
我想我需要有一個<xsl:for-each>
來列出有序列表中的步驟,因此SDCModule002將具有7個步驟,而SDCModule003將僅具有4個步驟,或者我只是獲取該對象的所有子節點的值?父<steps>
?
我不想執行15 if語句,例如“如果numSteps為1,則執行此操作。...如果numSteps為2,則執行此操作”等...我可以,它會起作用,但是我不確定是否存在是一種更有效的方法。
已編輯
預期的結果將是這樣的:
模塊:SCDModule001標題:測試受眾:我步驟:1.這樣做1 2.這樣做2 3.這樣做3 4.這樣做4 6.這樣做5 7.這樣做6 8.這樣做7
在該step?
上方包含xsl:for-each
step?
元素可能如下所示。 我還更改了一些xsl:apply-templates
以在輸出中顯示所有module/name
。 輸出效果不佳,但應該為您提供一個良好的開端。 別忘了添加
<?xml-stylesheet href="test.xslt" type="text/xsl" ?>
行到您的XML以在瀏覽器中運行它。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text()" />
<xsl:template match="/xml">
<html>
<head>
<title>***<xsl:value-of select="module/name[@ID='SDCModule002']/title "/>***</title>
</head>
<body>
<xsl:apply-templates select="module/name"/>
</body>
</html>
</xsl:template>
<xsl:template match="steps">
<div id= "info">
<center>
<font face="verdana" size="2">
<b>
<center>
<xsl:value-of select="../title" />
</center>
</b>
</font>
<hr />
<xsl:value-of select="../audience" />
<p></p>
<xsl:value-of select="../numSteps" />
<ol>
<xsl:for-each select="*">
<li><xsl:value-of select="." /></li>
</xsl:for-each>
</ol>
</center>
</div>
</xsl:template>
</xsl:stylesheet>
在Firefox中,輸出如下所示:
根據經驗,不需要時應避免使用xsl:for-each。 盡管這並不重要,但是對於小型翻譯或項目而言,還是明智的選擇。 apply-templates的附加好處是,如果在實現過程中更改了輸入,則只需更改xpath。
這是一個為每個模塊創建2個html文件的示例。 與“步驟”匹配的模板是額外的,您無需使其生效。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/xml">
<xsl:apply-templates select="module/name"/>
</xsl:template>
<!--Each name is a file - but can easily changed to module -->
<xsl:template match="name">
<html>
<head>
<title>***<xsl:value-of select="@ID"/>***</title>
</head>
<body>
<div id= "info">
<center>
<font face="verdana" size="2">
<b>
<center>
<xsl:value-of select="title" />
</center>
</b>
</font>
<hr />
<xsl:value-of select="audience" />
<p></p>
<xsl:apply-templates select="steps"/>
</center>
</div>
</body>
</html>
</xsl:template>
<!--Extra match for styling - steps can also directly be called from "name" template-->
<xsl:template match="steps">
<xsl:value-of select="numSteps" />
<ol>
<xsl:apply-templates select="node()" mode="steps"/>
</ol>
</xsl:template>
<!--Wildcard - matches all step nodes-->
<xsl:template match="*" mode="steps">
<li><xsl:value-of select="." /></li>
</xsl:template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.