更新属性名称和元素值

时间:2014-12-02 03:52:31

标签: xml xslt xslt-1.0

我需要使用地址行1更新地址,并将内部值更新为1 Jane,并将其保存在变量中。输入是任何虚拟xml

使用了这个样式表

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:template match="/">
        <xsl:variable name="request">
            <customers>
                <customer name="Address">1 Doe Place</customer>
                <customer name="State">OH</customer>
                <customer name="Name">John</customer>
                <customer name="Name">Kevin</customer>
                <customer name="Name">Leon</customer>
                <customer name="Name">Adam</customer>
                <customer name="city">Columbus</customer>
            </customers>
        </xsl:variable>
        <xsl:variable name="response">
        -------
        </xsl:variable>
        <xsl:copy-of select="$response"/>
    </xsl:template>
</xsl:stylesheet>

不知道到底要更新到底是什么。我知道如何处理身份变换,但这里我很困惑

1 个答案:

答案 0 :(得分:0)

也许这样的事情对你有用(我仍然对这是什么感到困惑):

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="request">
    <customers>
        <customer name="Address">1 Doe Place</customer>
        <customer name="State">OH</customer>
        <customer name="Name">John</customer>
        <customer name="Name">Kevin</customer>
        <customer name="Name">Leon</customer>
        <customer name="Name">Adam</customer>
        <customer name="city">Columbus</customer>
    </customers>
</xsl:variable>

<xsl:variable name="response">
    <customers>
        <xsl:for-each select="exsl:node-set($request)/customers/customer">
            <xsl:choose>
                <xsl:when test="@name='Address'">
                    <xsl:copy>
                        <xsl:copy-of select="@*"/>
                        <xsl:text>1 Jane place</xsl:text>
                    </xsl:copy>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="."/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </customers>
</xsl:variable>

<xsl:template match="/">
    <xsl:copy-of select="$response"/>
</xsl:template>

</xsl:stylesheet>

结果,应用于任何XML输入时:

<?xml version="1.0" encoding="UTF-8"?>
<customers>
   <customer name="Address">1 Jane place</customer>
   <customer name="State">OH</customer>
   <customer name="Name">John</customer>
   <customer name="Name">Kevin</customer>
   <customer name="Name">Leon</customer>
   <customer name="Name">Adam</customer>
   <customer name="city">Columbus</customer>
</customers>