2015-10-15 20 views
0

這對XML是XML:如何追加值使用XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<Posttype1 xmlns="http://www.company.com/path" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <type1Set> 
    <type1 action="Add"> 
     <CLASS maxvalue="type1">type1</CLASS> 
     <CREATEDBY>user</CREATEDBY> 
     <LANGCODE>EN</LANGCODE> 
     <STATUS>NEW</STATUS> 
     <ID>1073</ID> 
    </type1> 
    </type1Set> 
</Posttype1> 

在該XML Posttype1已轉換爲Puttype1和ID值已被附加有99 輸出應該是

<?xml version="1.0" encoding="UTF-8"?> 
<Puttype1 xmlns="http://www.company.com/path" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <type1Set> 
    <type1 action="Add"> 
     <CLASS maxvalue="type1">type1</CLASS> 
     <CREATEDBY>user</CREATEDBY> 
     <LANGCODE>EN</LANGCODE> 
     <STATUS>NEW</STATUS> 
     <ID>991073</ID> 
    </type1> 
    </type1Set> 
</Posttype1> 

Posttype1使用xslt進行轉換,但是當試圖追加99時,我無法到達Id。

使用的XSLT是

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:comp="http://www.company.com/path" version="1.0" > 
<xsl:template match="comp:Postype1 "> 
    <xsl:element name="Puttype1 "> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:element> 
</xsl:template> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 
<xsl:template match="Puttype1/type1Set/type1/ID"> 
<xsl:number>99</xsl:number> 
</xsl:template> 
</xsl:stylesheet> 

這給下面的輸出

​​

正如你所看到的xmlns和xmlns:XSI正在向子標籤,誰能告訴我,爲什麼這個正在發生,我不知道如何追加整數。

P.S.我也試過

<xsl:template match="comp:Postype1 "> 
<xsl:element name="Puttype1 "> 
    <xsl:apply-templates select="@*|node()"/> 
</xsl:element> 
</xsl:template> 

它將xmlns標籤移動到父級,但xmlns:xsi仍然在子級。

+0

''是不是在XSLT有效。元素必須是空的。如果你想用'xsl:number'來插入一個明確的數字,它需要是''。 –

回答

1

如何:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:comp="http://www.company.com/path"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="comp:Postype1 "> 
    <Puttype1 xmlns="http://www.company.com/path"> 
     <xsl:apply-templates select="@*|node()"/> 
    </Puttype1> 
</xsl:template> 

<xsl:template match="comp:ID"> 
    <xsl:copy> 
     <xsl:value-of select="concat('99', .)"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

謝謝。有效!! – user3420395

+0

有沒有一種方法可以追加99只當ID不是以99開始,長度大於2. – user3420395

+0

當然。用' 2]」>'開始模板。 –