获取 XSLT 中 child 个节点的值
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 个步骤,或者我只是获取所有 child parent <steps>
?
的节点
我不想做 15 if 语句说 "if numSteps is 1, then do this.... if numSteps is 2, then do that" 等等...我可以而且它会起作用,但我不确定是否有更有效的方法来做到这一点。
已编辑
预期的结果是这样的:
模块:SCDModule001
标题:测试
听众:我
脚步:
1. 这样做 1
2. 这样做 2
3. 这样做 3
4. 这样做 4
6. 这样做 5
7. 这样做 6
8. 这样做 7
在 step?
元素上方合并 xsl:for-each
可能如下所示。我还更改了一些 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 的额外好处是,如果在实现过程中输入发生变化,您只需更改 xpaths。
这是一个示例,它为每个模块创建 2 个 html 文件。匹配 "steps" 的模板是额外的,您不需要它来使其工作。
<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>
我是 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 个步骤,或者我只是获取所有 child parent <steps>
?
我不想做 15 if 语句说 "if numSteps is 1, then do this.... if numSteps is 2, then do that" 等等...我可以而且它会起作用,但我不确定是否有更有效的方法来做到这一点。
已编辑
预期的结果是这样的:
模块:SCDModule001 标题:测试 听众:我 脚步: 1. 这样做 1 2. 这样做 2 3. 这样做 3 4. 这样做 4 6. 这样做 5 7. 这样做 6 8. 这样做 7
在 step?
元素上方合并 xsl:for-each
可能如下所示。我还更改了一些 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 的额外好处是,如果在实现过程中输入发生变化,您只需更改 xpaths。
这是一个示例,它为每个模块创建 2 个 html 文件。匹配 "steps" 的模板是额外的,您不需要它来使其工作。
<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>