更新属性名称和元素值
问题描述:
我需要用地址线1更新地址,并将里面的值更新为1个简单的地方并将其保存在一个变量中。输入任何虚拟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>
不知道究竟是什么在这里更新。我知道如何与身份做变换,但在这里我很困惑
答
也许这样的事情可能会为你工作(我仍然感到困惑关于这真是什么):
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>
为什么你哈在样式表中编码数据,而不是使用外部查找XML文档 - 当地址(或任何其他项目)更改时,您可以简单地更新它? – 2014-12-02 03:56:27
其实我正在读一个来自早期调用的变量。然后我需要更新它 – mnvbrtn 2014-12-02 04:03:36
更新来自哪里?此外,描述“*来自早期调用的变量*”并不清楚。这在XSLT 1.0中至关重要,因为变量可能包含节点集或结果树片段。 – 2014-12-02 04:17:42