【问题标题】:How to template match the first child node of the root node xslt如何模板匹配根节点xslt的第一个子节点
【发布时间】:2015-08-20 20:33:23
【问题描述】:

我已经用谷歌搜索了一个小时,但我找到的答案都没有解决这个问题。

这是我的 xml 的 sn-p

<?xml version="1.0" encoding="UTF-8"?>
<project>
        <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>foobar</groupId>
        <artifactId>superpom</artifactId>
        <version>0.1.0.5</version>
    </parent>

        <artifactId>common-parent</artifactId>
        <version>0.2.0.4-SNAPSHOT</version>
    <packaging>pom</packaging>


    <properties>
        <protostuff.version>1.0.7</protostuff.version>
        <version>2.0.12.0</version>
    </properties>

我想要将第一个“版本”节点的值替换为其他值。

<?xml version="1.0" encoding="UTF-8"?>
<project>
        <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>foobar</groupId>
        <artifactId>superpom</artifactId>
        <version>0.1.0.5</version>
    </parent>

        <artifactId>common-parent</artifactId>
        <version>THIS HAS CHANGED</version>
    <packaging>pom</packaging>


    <properties>
        <protostuff.version>1.0.7</protostuff.version>
        <version>2.0.12.0</version>
    </properties>

到目前为止,这是我的 xslt 文件

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="pReplacement" select="'THIS HAS CHANGED'"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="//version[2]">
        <xsl:value-of select="$pReplacement"/>
    </xsl:template>
</xsl:stylesheet>

我一直在玩“匹配”中的值,但没有任何效果。我试过“版本”、“/版本”、“版本[2]”。没有任何效果。我不知道这是否重要,但我在红帽服务器上使用 xsltproc 来运行转换。有人可以帮忙吗?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    使用

    <xsl:template match="/project/version[1]">
      <xsl:copy>
        <xsl:value-of select="pReplacement"/>
      </xsl:copy>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 1970-01-01
      • 2019-10-08
      • 1970-01-01
      • 2018-04-18
      相关资源
      最近更新 更多