<xsl:apply-templates select =“ @ * | node()” />在转换中的双重用法

9ilsdx 9rvj 0lo

我有一个转换(简化版)是这样的:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

     <xsl:template match="head">
        <xsl:element name="head">
            <xsl:element name="title">
                <xsl:text>Transformed document</xsl:text>
            </xsl:element>
        </xsl:element>
     </xsl:template>

    <xsl:template match="body">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet> 

转换样本HTML文档

<?xml version="1.0" encoding="iso-8859-1"?>
<html xml:lang="en" lang="en">
    <head>
        <title>Sample page</title>
        <link rel="stylesheet" href="/style.css" type="text/css"/>
        <meta name="author" content="John Doe"></meta>
    </head>
    <body>
        <h1>Header</h1>
        <p>Some text</p>
        <p>Some other text</p>
    </body>
</html>

至:

<?xml version="1.0" encoding="UTF-8"?><html lang="en" xml:lang="en">
    <head><title>Transformed document</title></head>
    <body>
        <h1>Header</h1>
        <p>Some text</p>
        <p>Some other text</p>
    </body>
</html>

我不明白为什么我需要两次调用apply-templates。据我了解,第一个应将所有内容复制到输出(包括head,应将其覆盖),第二个应仅复制正文(这是我想要的)。但是,两者似乎都是必要的。如果我从正文中删除该文本,则输出中只有头,如果我删除第一个,则仅从节点中提取文本,而不是从节点中提取文本。

为什么在下面的示例中<xsl:apply-templates select="@* | node()"/>需要两次,并且可以简化该示例?

michael.hor257k

这里的问题不是调用xsl:apply-templates两次,而是有一个多余的整个模板。

由于您没有body进行任何更改,因此可以让它由身份转换模板(您的第一个模板)处理,然后删除最后一个模板:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

     <xsl:template match="head">
        <xsl:element name="head">
            <xsl:element name="title">
                <xsl:text>Transformed document</xsl:text>
            </xsl:element>
        </xsl:element>
     </xsl:template>

</xsl:stylesheet> 

顺便说一句,您还可以将模板匹配简化head为:

<xsl:template match="head">
    <head>
        <title>Transformed document</title>
    </head>
</xsl:template>

xsl:element如果事先知道元素名称,则无需使用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

<xsl:apply-templates select=".//text"/> 在 xsl 文件中是什么意思

使用 xsl:apply-templates 在特定结构中循环

如何从xsl:apply-templates调用XSLT中的外部Java函数

xsl:template / xsl:apply-templates 删除子节点

了解带有xsl:apply-templates的xsl:copy(XSLT 2.0)

<xsl:apply-templates>不适用于<xsl:attribute>之后的子代

<xsl:sort> 不能与 <xsl:apply-templates> 一起以表格形式显示数据

如何在另一个模板中使用 <Xsl: apply-templates match="othertemplate"> <xsl:template match="sometemplate">

xsl:apply-templates 什么都不返回——我错过了什么?

xsl:apply-templates如何正常工作,为什么将名称写2次?

如何在xsl:apply-templates中使用从Java设置的参数?

如何在多个条件下调用xsl:apply-templates

如何在 xsl:apply-templates 中使用变量作为值?

Xsl:apply-templates 选择问题,需要指定我拉的是哪一年

apply-templates和上下文项中的doc()

编译的XSL转换的用法

XSLT:如何编写一个<xsl:apply-templates />以匹配具有模式和不具有模式的模板的节点,而又不影响顺序?

如何在xsl中的select中使用变量

For-Each Select 子句 XSL 中的大于条件

Select last XML node

使用xsl:for-each循环而不是xsl:variable中的select =“ expr”进行查询时遇到的问题

在node.js / express中处理<select>表单参数

替换或不替换-xsl 1.0结果中的递归调用模板或Apply-template树碎片烦人

XSL的XSL转换

apply-templates输出内容的次数比预期的多

在LINQ中重用Lambda Select片段

保存列轉換以在 SELECT 中重用

如何使用select和when在xsl-xml中显示产品

在<xsl:param name = select> xsl中使用多个