Xml XSLT需要非常简单的转换
我需要在xml文件上运行转换。这将是非常基本的,但在我有点不知所措之前,我从未做过任何xslt工作。我已经做了很多这样的问答,但都没能解决 我需要的是我的xml文件有一个模式引用,我需要将其更改为另一个模式引用Xml XSLT需要非常简单的转换,xml,xslt,Xml,Xslt,我需要在xml文件上运行转换。这将是非常基本的,但在我有点不知所措之前,我从未做过任何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/XMLS
<?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,并保持文件的其余部分不变?听起来很简单,但我似乎无法理解这一点?我在这里尝试了一个简单的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标记
感谢您在广告中使用:
<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>
新的\u模式\u位置
应用于提供的XML产品
<Schedule xsi:schemaLocation="new_schema_location" xmlns="ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Interface_Header>
</Interface_Header>
...
</Schedule>
...
当您不需要新的架构位置来拥有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()=…])比不太特定的路径(@*
)具有更高的优先级。非常感谢。我在理解xmlns:ns=“new_namespace”xmlns=“ns”部分时遇到了一些困难,但我更改了处的值,这对我来说很有效。我想,我还得多读一些这方面的书。无论如何,非常感谢。