参数传递值到模板
<chapter>
<title>头部标题数据</title>
<section id='0'>
<title>section顶级</title>
<section id='1'>
<title>section一级</title>
<section id='2'>
<title>section二级</title>
<section id='3'>
<title>section三级</title>
</section>
<section id='3'>
<title>section三级</title>
<v>s级</v>
</section>
</section>
</section>
<section id='0-1'>
<title>section同级</title>
</section>
</section>
<section id='0'>
<title>section同级</title>
</section>
</chapter>
<!-- 匹配id属性值为3的section元素 -->
<xsl:template match="section[@id='3']">
<xsl:value-of select="title"/> <!-- .会保留格式 -->
<!-- 输出换行符以便区分不同项 -->
<xsl:text> </xsl:text>
</xsl:template>
<!-- 忽略其他所有元素 -->
<xsl:template match="@*|node()">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<root>
<element1>内容1</element1>
<element2>内容2</element2>
</root>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<output>
<xsl:value-of select="element1"/>
</output>
</xsl:template>
</xsl:stylesheet>
使用第一种 <xsl:value-of select="element1"/> 结果<output>内容1</output>
使用第二种 <xsl:value-of select="."/>结果<output>内容1</output>结果<output>内容2</output>
这将输出当前节点的文本内容(如果有的话)。
.//当前级往下找两个节点
../向上找一级
换行符
<xsl:text> </xsl:text> <!-- 或者写成 <xsl:text>
</xsl:text> -->
ancestor用法它从当前节点的父节点开始一路向上遍历到根节点为止
<?xml version="1.0" encoding="UTF-8"?>
<library>
<branch location="北京分馆">
<book id="bk101">
<!-- 其他书的信息 -->
</book>
</branch>
</library>
<xsl:template match="book">
<output>
<xsl:value-of select="ancestor::branch/@location"/>
</output>
</xsl:template>
<xsl:call-template name="process-data">
<xsl:with-param name="data" select="'Hello, World!'"/>
</xsl:call-template>
<xsl:template name="process-data">
<xsl:param name="data"/>
<!-- 在这里处理数据 -->
<xsl:value-of select="$data"/>
</xsl:template>
<xsl:template match="parent-element">
<xsl:apply-templates select="child-element">
<xsl:with-param name="myParam" select="'Some Value'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="child-element">
<xsl:param name="myParam"/>
<!-- 使用参数的值 -->
<xsl:value-of select="$myParam"/>
<!-- 其他处理代码 -->
</xsl:template>
元素来赋值和创建变量
<xsl:variable name="myVariable">
<xsl:value-of select="/root/element"/>
</xsl:variable>
<xsl:variable name="myVariable" select="'Hello, World!'"/>
<xsl:variable name="sum">
<xsl:value-of select="sum(/root/numbers/number)"/>
</xsl:variable>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:if test="contains(@tou, 'value')">
<xsl:attribute name="new-attribute">new-value</xsl:attribute>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template match="element[@attribute]">
<xsl:choose>
<xsl:when test="contains(@attribute, 'at') or contains(@attribute, 'bt')">
<xsl:text>The attribute value contains 'at' or 'bt'</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>The attribute value does not contain 'at' or 'bt'</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
变量调用
<xsl:value-of select="$sum"/>
<xsl:value-of select="$myVariable"/>
杂项使用
<xsl:value-of select="test/parent::node()/@name"/>
<xsl:value-of select="@name"/>
<xsl:value-of select="/pat/tv/name"/>
<xsl:value-of select="label_people/pm/country"/>
<xsl:template match="Major/Class" mode="student">
<xsl:value-of select="Classpresident/pname"/>
</xsl:template>
<xsl:if test="@id =2"></xsl:if>
<xsl:param name="count" select="count(/label_out/label_people)"/>
<xsl:variable name="countabc">
<xsl:number/>
</xsl:variable>
<xsl:value-of select="$count" />
<xsl:value-of select="$countabc" />
高级扩展
public class MyJavaClass {
public String myMethod(String input) {
// 在这里实现你的逻辑
return "Hello, " + input;
}
}
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="com.xiaoxun.emlsr.xml.a002.MyJavaClass"
extension-element-prefixes="my">
<xsl:template match="/">
<xsl:value-of select="my:myMethod('from Java')"/>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>My XSLT Example</title>
<script>
function myFunction() {
// 在这里编写JavaScript代码
var message = "Hello, World!";
alert(message);
}
</script>
</head>
<body>
<h1>My XSLT Example</h1>
<button onclick="myFunction()">Click me</button>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<xsl:template>:
match:指定该模板匹配XML源文档中的哪些元素或路径。
mode:可选属性,用于指定模板的处理模式。
priority:可选属性,用于指定模板的优先级,当多个模板匹配同一节点时使用。
<xsl:apply-templates>:
select:指定要应用模板的节点集,通常是一个XPath表达式。
mode:可选属性,指定应用模板的处理模式。
<xsl:choose>、<xsl:when> 和 <xsl:otherwise>:
用于条件逻辑,类似于编程语言中的if...else语句。
<xsl:for-each>:
select:指定要遍历的节点集,通常是一个XPath表达式。
<xsl:value-of>:
select:指定要提取值的XPath表达式。
<xsl:copy> 和 <xsl:copy-of>:
copy复制一个元素及其所有属性和子元素,而copy-of复制一个节点集。
<xsl:param> 和 <xsl:variable>:
name:定义参数或变量的名称。
select:可选属性,为变量指定一个初始值。
<xsl:attribute>:
name:指定要添加的属性名称。
select:可选属性,指定属性值。
<xsl:call-template>:
name:要调用的模板的名称。
with-param:可选元素,用于向模板传递参数。
<xsl:comment>:
内容:生成XML注释。
<xsl:text>:
内容:生成文本节点。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/200902.html