XSLT根据另一个节点的值更改值节点

XSLT根据另一个节点的值更改值节点,xslt,Xslt,我有一个XML,类似这样: <?xml version="1.0" encoding="UTF-8"?> <earth> <computer> <parts> <cpu>AMD;fast</cpu> <video>GF</video> <power&g

我有一个XML,类似这样:

 <?xml version="1.0" encoding="UTF-8"?>
       <earth>
     <computer>
            <parts>
                <cpu>AMD;fast</cpu>
                <video>GF</video>
                <power>slow</power>
                ...others
            </parts>
            <owner>
             <name>Frank</name>
            <owner>
          </computer>

    <earth>

AMD;快速的
女朋友
缓慢的
……其他
直率的
我想要创建xsl转换(xsl:stylesheet version=“2.0”xmlns:xsl=”http://www.w3.org/1999/XSL/Transform"). 仅当cpu有符号“;”时,我的预期结果幂应更改为“;”之后的值,如果没有‘;’结果应该不会改变

<earth>
 <computer>
        <parts>
            <cpu>AMD</cpu>
            <video>GF</video>
            <power>fast</power>
            ...others
        </parts>
        <owner>
          <name>Frank</name>
        <owner>
      </computer>
<earth>

AMD
女朋友
快速的
……其他
直率的
尝试这样做,但运气不佳:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions"
    exclude-result-prefixes="fn">
    <xsl:output encoding="utf-8" method="xml" indent="yes" />
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="parts">
        <xsl:choose>
            <!-- try test if node name equals 'power' if equals them try make logic 
                here, this dont work-->
            <xsl:when test="name() = 'power'">
                <xsl:variable name="text" select="./cpu" />
                <xsl:variable name="sep" select="';'" />
                <xsl:variable name="powerTable" select="tokenize($text, $sep)" />
                <xsl:value-of select="$powerTable[1]" />
            </xsl:when>
            <!--if not 'power' copy node -->
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()" />
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

我将使用以下模板来完成此任务:

<xsl:template match="parts[contains(cpu,';')]/power">
    <power>
        <xsl:value-of select="../cpu/substring-after(.,';')"/>
    </power>
</xsl:template>

<xsl:template match="cpu[contains(.,';')]">
    <cpu>
        <xsl:value-of select="substring-before(.,';')"/>
    </cpu>
</xsl:template>

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

第一个模板匹配
power
元素,该元素是
parts
parts/power
)的直接子元素,其中
parts
具有包含字符
的子元素
cpu
部件[包含(cpu,;)]
)。此模板将在字符
后输出
power
元素以及
cpu
元素值

第二个模板匹配包含字符的
cpu
元素
,并在字符
之前输出带有初始值的
cpu
元素


另一个模板就是标识模板,我想您知道它的用途。

“我有一个XML样式表”。我们可以知道它看起来怎么样吗?因为到目前为止发布的只是XMLsI编辑我的帖子,所以我有xml文件,需要创建xsl将此文件转换为新的xml文件,并获得预期的结果。到目前为止,您是否尝试编写XSLT?如果您有,请分享它,并解释哪些部分没有按预期工作,或者哪些部分您不知道如何实现。如果还没有,请阅读有关XSLT的介绍性教程,并尝试先自己编写一个。祝你好运为了得到回复,我为部件创建了模板,尝试在那个里测试节点名是否等于power,但这不起作用。所有其他节点都应为“复制”