无法匹配具有命名空间属性的 XML 元素 [英] Trouble matching XML elements that has namespace attribute

查看:29
本文介绍了无法匹配具有命名空间属性的 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要使用 xslt 在下面的 xml 中插入一段文本,条件语句会是什么样子?

How would the conditional statement look like if I'm to insert a section of text into the xml below using xslt?

<items xmlns="http://mynamespace.com/definition">
    <item>
        <number id="1"/>
    </item>
    <item>
        <number id="2"/>
    </item>
    <!-- insert the below text -->
    <reference>
        <refNo id="a"/>
        <refNo id="b"/>
    </reference>
    <!-- end insert -->
</items>

这就是我现在的 xsl 的样子(条件不对...):

This is how my xsl looks like at the moment (the condition is wrong...):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://mynamespace.com/definition"
    version="1.0">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:param name="addRef">
        <reference>
            <refNo id="a"/>
            <refNo id="b"/>
        </reference>
    </xsl:param>
    <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <!-- here is where the condition got stuck... -->
    <xsl:template match="/items[namespace-url()=*]/item[position()=last()]">
        <xsl:call-template name="identity"/>
        <xsl:copy-of select="$addRef"/>
    </xsl:template>
</xsl:stylesheet>

我想在最底部的 之后添加参考部分,但我遇到了如何绕过匹配具有(显式)命名空间的元素的问题.

I wanted to add the reference section after the bottom-most , but I got stuck with how to get around matching an element that has a (explicit) namespace.

谢谢.

推荐答案

试试这个:

match="//*[local-name()='items']/*[local-name()='item'][position()=last()]"

这篇关于无法匹配具有命名空间属性的 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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