2011-09-16 101 views
4

我想了解是否有方法使用XSLT修改xml文档,或者是否还有比XSLT更好的方法?使用XSLT修改xml文档

说,我有一个XML像这样:

<feed xmlns="http://www.w3.org/2005/Atom"> 
<id>http://libx.org/libx2/libapps</id> 
<entry> 
<id>http://libx.org/libx2/libapps/2</id> 
</entry> 
<entry> 
<id>http://libx.org/libx2/libapps/3</id> 
</entry> 
</feed> 

我愿做以下动作:

  1. 修改饲料:id来(除去饲料的文本:身份证)
  2. 修改条目:id值,例如“/”后面的最后一个数字值仍然存在。

结果XML看起来应该像这样:

<feed xmlns="http://www.w3.org/2005/Atom"> 
<id></id> 
<entry> 
<id>2</id> 
</entry> 
<entry> 
<id>3</id> 
</entry> 
</feed> 

感谢, 索尼

+0

好问题,1。是的,这在XSLT 1.0中很容易,在XSLT 2.0中很简单。 –

回答

4

I. XSLT 1.0解决方案:

这XSLT 1.0转换任何网址运作正常,而无需对哈氏一个共同的出发字符串的所有URL的任何假设:

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <id>http://libx.org/libx2/libapps</id> 
    <entry> 
     <id>http://libx.org/libx2/libapps/2</id> 
    </entry> 
    <entry> 
     <id>http://libx.org/libx2/libapps/3</id> 
    </entry> 
</feed> 

的:当所提供的XML文档应用

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://www.w3.org/2005/Atom"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="x:feed/x:id/node()"/> 

<xsl:template match="x:entry/x:id/text()" name="eatSlashes"> 
    <xsl:param name="pText" select="."/> 

    <xsl:choose> 
    <xsl:when test="not(contains($pText, '/'))"> 
    <xsl:value-of select="$pText"/> 
    </xsl:when> 
    <xsl:otherwise> 
    <xsl:call-template name="eatSlashes"> 
    <xsl:with-param name="pText" select= 
        "substring-after($pText, '/')"/> 
    </xsl:call-template> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

想要产生正确的结果

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <id/> 
    <entry> 
     <id>2</id> 
    </entry> 
    <entry> 
     <id>3</id> 
    </entry> 
</feed> 

二, XSLT 2.0溶液

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://www.w3.org/2005/Atom"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="x:feed/x:id/node()"/> 

<xsl:template match="x:entry/x:id/text()"> 
    <xsl:sequence select="tokenize(.,'/')[last()]"/> 
</xsl:template> 
</xsl:stylesheet> 

当对相同的XML文档(以上)应用中,相同的正确的结果产生

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <id/> 
    <entry> 
     <id>2</id> 
    </entry> 
    <entry> 
     <id>3</id> 
    </entry> 
</feed> 
0

我的XSLT的知识是不是最好的,但是这似乎工作:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

    <xsl:template match="a:feed/a:id" xmlns:a="http://www.w3.org/2005/Atom"> 
    <xsl:copy/> 
    </xsl:template> 
    <xsl:template match="a:entry/a:id" xmlns:a="http://www.w3.org/2005/Atom"> 
    <xsl:copy><xsl:value-of select="substring-after(.,'http://libx.org/libx2/libapps/')"/></xsl:copy> 
    </xsl:template> 

</xsl:stylesheet>