2015-10-15 36 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]”>'开始模板。 –