2014-12-02 127 views
0

我需要用地址線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> 

不知道究竟是什麼在這裏更新。我知道如何與身份做變換,但在這裏我很困惑

+0

爲什麼你哈在樣式表中編碼數據,而不是使用外部查找XML文檔 - 當地址(或任何其他項目)更改時,您可以簡單地更新它? – 2014-12-02 03:56:27

+0

其實我正在讀一個來自早期調用的變量。然後我需要更新它 – mnvbrtn 2014-12-02 04:03:36

+0

更新來自哪裏?此外,描述「*來自早期調用的變量*」並不清楚。這在XSLT 1.0中至關重要,因爲變量可能包含節點集或結果樹片段。 – 2014-12-02 04:17:42

回答

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>