Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 用xslt转换属性_Xml_Xslt_Xpath - Fatal编程技术网

Xml 用xslt转换属性

Xml 用xslt转换属性,xml,xslt,xpath,Xml,Xslt,Xpath,这是我使用XSLT的第二天,所以我完全是个新手。现在我想在计算中使用我的属性 我要编辑的XML如下所示: <position x="106" y="47" zIndex="6" width="30" height="5"/> <position x="106" y="56" zIndex="7" width="30" height="5"/> <position x="106" y="66" zIndex="8" width="30" height="5"/>

这是我使用XSLT的第二天,所以我完全是个新手。现在我想在计算中使用我的属性

我要编辑的XML如下所示:

<position x="106" y="47" zIndex="6" width="30" height="5"/>
<position x="106" y="56" zIndex="7" width="30" height="5"/>
<position x="106" y="66" zIndex="8" width="30" height="5"/>
<position x="106" y="75" zIndex="9" width="30" height="5"/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@x[parent::position]">
        <xsl:attribute name="x">
            <xsl:value-of select="sum((@x, 1000))"/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

我的XSLT代码如下所示:

<position x="106" y="47" zIndex="6" width="30" height="5"/>
<position x="106" y="56" zIndex="7" width="30" height="5"/>
<position x="106" y="66" zIndex="8" width="30" height="5"/>
<position x="106" y="75" zIndex="9" width="30" height="5"/>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@x[parent::position]">
        <xsl:attribute name="x">
            <xsl:value-of select="sum((@x, 1000))"/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

因此,在这个代码示例中,我想将x属性增加1000,但只是将它们设置为1000。
编辑:我希望能够使用至少所有数值计算和条件(指当前x值),如:
==
<代码>=<代码>>=<代码>=您想要的表达式是

<xsl:value-of select="sum((., 1000))"/>
注意,您还可以简化模板匹配,如下所示

<xsl:template match="position/@x">
    <xsl:attribute name="x">
        <xsl:value-of select=". + 1000"/>
    </xsl:attribute>
</xsl:template>

如果您确实在使用XSLT 2.0,则可以将其进一步简化为:

<xsl:template match="position/@x">
    <xsl:attribute name="x" select=". + 1000"/>
</xsl:template>

注意,要在匹配中添加条件,请使用方括号,如下所示

<xsl:template match="position/@x[. &lt; 1000]">


谢谢,效果很好!你对我的编辑有什么解决方案吗?老实说,我不完全理解你的要求。你可能需要举个例子。ThanksI的意思是:
@x<1000
因此属性
x
的值必须小于1000,这将受到计算的影响。啊,我明白了。您只需使用方括号将条件添加到模板匹配中即可