2012-02-22 95 views
1

我有一个需要在xml文件上运行转换。这将是非常基础的,但在我有点迷路之前从未做过任何xslt工作。我已经有很多这样的问题了,但是还没有能够解决这个问题?XSLT非常简单的转换需求

我所需要的是我的xml文件有一个模式引用,我需要将其更改为不同的模式引用。

<?xml version="1.0" encoding="UTF-8"?> 
<Schedule xmlns="http://www.xxx.com/12022012/schedule/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxx.com/12022012/schedule/v2 ../Schema/Schema_v2.xsd"> 
    <Interface_Header> 
    </Interface_Header> 
... 
</Schedule> 

我只是想改变V2的V3到V3,并保持文件的其余部分完好?这听起来很简单,但我似乎无法弄清楚这一点?我想一个简单的XSLT这里: -

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

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

但使用此我所有的值输出没有任何XML标记。

thanks in adv。

回答

0

用途:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:ns="new_namespace"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="@xsi:schemaLocation"> 
     <xsl:attribute name="xsi:schemaLocation"> 
      <xsl:text>new_schema_location</xsl:text> 
     </xsl:attribute> 
    </xsl:template> 

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

    <xsl:template match="*"> 
     <xsl:element name="{local-name()}" namespace="ns"> 
      <xsl:apply-templates select="node() | @*"/> 
     </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

应用提供XML产生

<Schedule xsi:schemaLocation="new_schema_location" xmlns="ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Interface_Header> 
    </Interface_Header> 
    ... 
</Schedule> 
+0

非常感谢。我在理解xmlns时遇到了一些困难:ns =“new_namespace”xmlns =“ns”piece,但我更改了的值,我。将不得不在我认为的这个问题上做更多的阅读。无论如何非常感谢。 – Purplemonkey 2012-02-22 17:12:05

+0

@Purplemonkey,欢迎光临。 – 2012-02-22 17:14:46

0

当你并不需要新的架构位置有XSI的命名空间,那么下面的工作:

<xsl:output indent="yes" method="xml"/> 

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

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

<xsl:template match="@*[local-name() = 'schemaLocation']"> 
    <xsl:attribute name="schemaLocation">newSchemaLocation</xsl:attribute> 
</xsl:template> 

当你确实需要再次使用xsi命名空间时,当然需要在模板,因此必须在样式表头中声明,如下所示,其中local-name()函数被替换为name()函数;前者包括命名空间,其中后者没有:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 

... 

    <xsl:template match="@*[name() = 'xsi:schemaLocation']"> 
     <xsl:attribute name="xsi:schemaLocation">newSchemaLocation</xsl:attribute> 
    </xsl:template> 

</xsl:stylesheet> 

注意,这两个解决方案依赖于特定的模板路径(@*[local-name()=...])具有更高的优先级比不太具体的(@*)。