在 XSLT 中获取子节点的值 [英] Get value of child nodes in XSLT
问题描述
我对 XML 和 XSLT 非常陌生,但我正在尝试创建一个 HTML 页面,该页面循环执行不同长度的步骤并生成一个 HTML 列表.
XML 看起来像这样:
我有它,所以我正在搜索的模块会显示标题、受众、任务等信息,以便有效.
<xsl:template match="/xml"><头><title><xsl:value-of select="module/name[@ID='SDCModule001']/title"/></title>头部><xsl:apply-templates select="module/name[@ID='SDCModule001']"/></html></xsl:模板><xsl:template name="stepList" match="name"><div id="信息"><中心><font face="verdana" size="2"><b><center><xsl:value-of select="title"/></center></b></字体><hr></hr><xsl:value-of select="audience"/><p></p><xsl:value-of select="numSteps"/></中心>
</xsl:模板></xsl:stylesheet>
我想我需要一个 <xsl:for-each>
列出有序列表中的步骤,所以 SDCModule002 将有 7 个步骤,而 SDCModule003 将只有 4 个,或者做我只是得到父
的所有子节点的值?
我不想做 15 条 if 语句说如果 numSteps 是 1,那么做这个......如果 numSteps 是 2,那么做那个"等等......我可以而且它会工作,但我不是确定是否有更有效的方法来做到这一点.
已编辑
预期结果如下:
模块:SCDModule001标题:测试观众:我脚步:1. 这样做 12. 做这个 23. 这样做 34. 做这个 46. 这样做 57. 这样做 68. 做这个 7
在 step?
元素上方合并 xsl:for-each
元素可能如下所示.我还更改了一些 xsl:apply-templates
以在输出中显示所有 module/name
.输出不是很好,但应该给你一个好的开始.不要忘记添加
添加到您的 XML 以在浏览器中运行它.
<xsl:template match="text()"/><xsl:template match="/xml"><头><title>***<xsl:value-of select="module/name[@ID='SDCModule002']/title "/>***</title>头部><身体><xsl:apply-templates select="module/name"/></html></xsl:模板><xsl:template match="steps"><div id="信息"><中心><font face="verdana" size="2"><b><中心><xsl:value-of select="../title"/></中心></b><小时/><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></中心>
</xsl:模板></xsl:stylesheet>
在 Firefox 中,输出如下所示:
I am very new to XML and XSLT but I am trying to create a HTML page that loops through a varying length of steps and produces a list in HTML.
The XML looks something like this:
<?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>
I have it so the information like Title, audience, task etc. are displayed for the module I am searching so that works.
<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>
I think I need to have a <xsl:for-each>
that lists the steps in an ordered list, so SDCModule002 will have 7 steps, and SDCModule003 will only have 4, or do I just get the values of all child nodes of the parent <steps>
?
I don't want to do 15 if statements saying "if numSteps is 1, then do this.... if numSteps is 2, then do that" etc... I can and it would work but I am not sure if there is a more efficient way of doing this.
Edited
The expected results would be like this:
Module: SCDModule001 Title: Test Audience: Me Steps: 1. Do this 1 2. Do this 2 3. Do this 3 4. Do this 4 6. Do this 5 7. Do this 6 8. Do this 7
Incorporating an xsl:for-each
above the step?
elements could look like the following. I also changed some the xsl:apply-templates
to show all module/name
s in the output. The output is not pretty, but should give you a good start. Don't forget to add an
<?xml-stylesheet href="test.xslt" type="text/xsl" ?>
line to your XML to run it in the browser.
<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>
In Firefox, the output looks like this:
这篇关于在 XSLT 中获取子节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!