根据包含文件更改 XSLT 的节点名称 [英] Change Node name of an XSLT based on an include file
本文介绍了根据包含文件更改 XSLT 的节点名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个需要转换 XML 文件的 XSLT 文件.
Hi I have a XSLT file which need to transform an XML file.
示例 XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Document>
<Customer>
<Header>
<Message>
<xsl:value-of select="//Header/Message"/>
</Message>
</Header>
</Customer>
</Document>
</xsl:Template>
<xsl:include href="Inc1.xsl" />
</xsl:stylesheet>
现在我需要的是根据底部的包含文件名将节点名称 Customer 更改为供应商,还有一件事,我还根据包含文件为 Document 节点设置了特定属性.
Now what I need is to change the node name Customer to Supplier depending on the include filename at the bottom and one more thing, I have specific attributes for the Document node also depending on the include file.
谢谢,希望有人能帮我.
Thanks and hope someone can help me out.
推荐答案
唯一可行的机制是让包含的样式表处理特定的元素名称和 <Document> 属性:像这样:
The only viable mechanism is to have the included stylesheet handle the specific element names and <Document> attributes: Something like this:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Document>
<xsl:call-template name="apply-document-attributes" />
<xsl:element name="{$customer-element-name}">
<Header>
<Message>
<xsl:value-of select="//Header/Message" />
</Message>
</Header>
</xsl:element>
</Document>
</xsl:template>
<xsl:include href="Inc1.xsl" />
</xsl:stylesheet>
和包含的样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="customer-element-name" select="'Supplier'"/>
<xsl:template name="apply-document-attributes">
<xsl:attribute name="foo">bar</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
或者,
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="customer-element-name" select="'Customer'"/>
<xsl:template name="apply-document-attributes"/>
</xsl:stylesheet>
这篇关于根据包含文件更改 XSLT 的节点名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文