将模板应用于具有具有属性的子节点的节点 [英] Apply template to nodes that have children with attribute

查看:23
本文介绍了将模板应用于具有具有属性的子节点的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将模板应用于具有指定属性的子节点的节点,我很好奇是否可以使用 <template match=...

I'd like to apply a template to nodes that have children with a specified attribute, and I'm curious if it's possible with a <template match=...

所以如果我有一个输入 xml

So if i have an input xml

<?xml version="1.0"?>
<parent-node>
    <child-node>
        <label>value1</label>
        <name>name1</name>
        <desc src="anything">description1</desc>
    </child-node>
    <child-node>
        <label>value2</label>
        <desc>description2</desc>
    </child-node>
    <some-node>
        <name>name3</name>
        <desc src="something">description3</desc>
    </some-node>
</parent-node>

所需的模板将应用于具有 desc 子节点并定义了 src 属性的节点,例如.第一个和最后一个节点:

the required template will be applied to the nodes that have desc children with src attribute defined, eg. the first and last nodes:

<child-node>
    <label>value1</label>
    <name>name1</name>
    <desc src="anything">description1</desc>
</child-node>
<some-node>
    <name>name3</name>
    <desc src="something">description3</desc>
</some-node>

到目前为止我拥有的最好的是一个模板匹配具有 desc 子节点的节点,其余的(测试是否有任何 desc 节点具有 @src) 位于模板内,位于 xsl:choose 子句中:

The best i have so far is a template matching the nodes that have desc children, and the rest (testing if any of the desc nodes have @src) is inside the template, in an xsl:choose clause:

<xsl:template match="*[desc]">
    <xsl:choose>
        <xsl:when test="desc[@src]">
            <xsl:element name="node-with-src">
                [...]
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <xsl:element name="node">
                [...]
            </xsl:element>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

有了这样一个模板,我可以离开这个模板中的 otherwise 部分,无论如何,它会更好.

By having such a template, i could leave but the otherwise part inside this template, and anyway, it would be a lot nicer.

预先感谢您的每一个回答!

Thank you in advance for every answer!

编辑我更喜欢 1.0 解决方案,但这不是标准.

Edit I'd prefer a 1.0 solution, but it's not a criteria.

推荐答案

允许使用更复杂(嵌套)的谓词.使用这个:

More complex (nested) predicates are allowed. Use this:

<xsl:template match="*[desc[@src]]">

以及没有src属性的节点的相应模板:

And a corresponding template for the nodes without a src attribute:

<xsl:template match="*[desc[not(@src)]]">

例如:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="*[desc[@src]]"> 
        <xsl:element name="node-with-src"/>
    </xsl:template>
    <xsl:template match="*[desc[not(@src)]]">
        <xsl:element name="node"/>
    </xsl:template>
</xsl:stylesheet>

适用于:

<parent-node>
    <child-node>
        <label>value1</label>
        <name>name1</name>
        <desc src="anything">description1</desc>
    </child-node>
    <child-node>
        <label>value2</label>
        <desc>description2</desc>
    </child-node>
    <some-node>
        <name>name3</name>
        <desc src="something">description3</desc>
    </some-node>
</parent-node>

输出:

<node-with-src/>
<node/>
<node-with-src/>

这篇关于将模板应用于具有具有属性的子节点的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆