XML 到 XML xsl 样式表
我的任务是研究使用 xsl 来更新工作中的一些 xml 文档,并且我一直在查看一些 xsl 教程,尽管我还没有遇到我理想中想要寻找的内容。 由于我不在工作,
这是我正在寻找的一个小例子:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<id>627</id>
<name>application1</name>
<url>www.application.com</url>
</application>
我需要将其转换为:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<id>627</id>
<application_name>application1</application_name>
<url>www.application.com</url>
</application>
现在从我看到的示例和教程中,我可以使用硬编码的 xsl 表来完成此操作,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<application>
<xsl:apply-templates/>
</application>
</xsl:template>
<xsl:template match="id">
<id>
<xsl:value-of select="id"/>
<xsl:apply-templates/>
</id>
</xsl:template>
<xsl:template match="name">
<application_name>
<xsl:value-of select="name"/>
<xsl:apply-templates/>
</application_name>
</xsl:template>
<xsl:template match="url">
<url>
<xsl:value-of select="url"/>
<xsl:apply-templates/>
</url>
</xsl:template>
</xsl:stylesheet>
但这确实不切实际,因为我们有近 50 个不同的 xml 文档可能需要更改,所以我真的在寻找一个可以使用的包罗万象的模板,然后只覆盖需要更改的适当元素。
xsl中有这个功能吗?
I've been given the task to look into using xsl to update a few of our xml documents at work, and I have been looking at some tutorials for xsl, though I have yet to come across what I would ideally be looking for...
Since im not at work, wheres a small example of heres what im looking for:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<id>627</id>
<name>application1</name>
<url>www.application.com</url>
</application>
I would need to convert this to:
<?xml version="1.0" encoding="UTF-8"?>
<application>
<id>627</id>
<application_name>application1</application_name>
<url>www.application.com</url>
</application>
Now from the examples and tutorials I've seen I could do this with a hard coded xsl sheet looking like so:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="UTF-8"/>
<xsl:template match="/">
<application>
<xsl:apply-templates/>
</application>
</xsl:template>
<xsl:template match="id">
<id>
<xsl:value-of select="id"/>
<xsl:apply-templates/>
</id>
</xsl:template>
<xsl:template match="name">
<application_name>
<xsl:value-of select="name"/>
<xsl:apply-templates/>
</application_name>
</xsl:template>
<xsl:template match="url">
<url>
<xsl:value-of select="url"/>
<xsl:apply-templates/>
</url>
</xsl:template>
</xsl:stylesheet>
But this really wouldnt be practical since we have near 50 different xml documents that might need changing, and so I was really looking for a catch all template that I can use, and then only override the appropriate element that needs to be changed.
Does this functionality exist in xsl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我看看我是否了解您的需求。下面转换中的第一个模板规则称为身份转换,通过复制来处理所有节点,并且可以针对单个元素、属性、注释、处理指令或文本进行覆盖节点。在你的例子中,我刚刚覆盖了元素
name
。XSLT 1.0 在 Saxon 6.5.5 下测试
应用于此输入:
产生:
Let me see if I understand what you need. The first template rule in the transform below is called identity transform processes all nodes by copying them, and can be overridden for individual elements, attributes, comments, processing instructions, or text nodes. In your case, I've just overridden for the element
name
.XSLT 1.0 tested under Saxon 6.5.5
Applied on this input:
Produces:
你可以尝试这样的事情:
You could try something like this: