Xml 自动反转xsl
假设我编写了一个简单的xslt,可以将一条消息转换为另一条消息,有没有办法自动生成相反的消息 原始XSLTXml 自动反转xsl,xml,xslt,code-generation,reverse,inverse,Xml,Xslt,Code Generation,Reverse,Inverse,假设我编写了一个简单的xslt,可以将一条消息转换为另一条消息,有没有办法自动生成相反的消息 原始XSLT <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <hello> <xsl:for-each select="/hello/greeting"&g
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<hello>
<xsl:for-each select="/hello/greeting">
<H1>
<xsl:value-of select="."/>
</H1>
</xsl:for-each>
</hello>
</xsl:template>
</xsl:stylesheet>
所需XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<hello>
<xsl:for-each select="/hello/H1">
<greeting>
<xsl:value-of select="."/>
</greeting>
</xsl:for-each>
</hello>
</xsl:template>
</xsl:stylesheet>
我认为没有一种方法可以自动为反向生成一个 但是,如果模板的作用相同,则可以重用当前XSL中的大部分内容 在你的例子中是
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<hello>
<xsl:for-each select="/hello/H1 | /hello/greeting">
<H1>
<xsl:value-of select="."/>
</H1>
</xsl:for-each>
</hello>
</xsl:template>
</xsl:stylesheet>
不,没有通用的方法来反转XSLT。事实上,我认为大多数XSLT是不可逆的。但是,可以通过更改参数值来设计上述示例,使其可以在正向或反向运行:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:param name="direction" select="'forward'" />
<xsl:variable name="mappingNF">
<map from="greeting" to="H1" />
<map from="pleasantry" to="H2" />
</xsl:variable>
<xsl:variable name="mapping" select="exslt:node-set($mappingNF)" />
<xsl:template match="@* | node()" priority="-1" name="Copy">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" mode="copy">
<xsl:call-template name="Copy" />
</xsl:template>
<xsl:template match="*">
<xsl:variable name="mappingItem"
select="$mapping/*[$direction = 'forward' and @from = local-name(current()) or
$direction = 'reverse' and @to = local-name(current())]" />
<xsl:apply-templates select="current()[$mappingItem]" mode="rename">
<xsl:with-param name="mappingItem" select="$mappingItem" />
</xsl:apply-templates>
<xsl:apply-templates select="current()[not($mappingItem)]" mode="copy" />
</xsl:template>
<xsl:template match="*" mode="rename">
<xsl:param name="mappingItem" />
<xsl:element name="{$mappingItem/@to[$direction = 'forward'] |
$mappingItem/@from[$direction = 'reverse']}">
<xsl:apply-templates select ="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
mappingNF
表示节点名称之间的对应关系,可以根据需要使用任意多的map
s来增加。转换的方向可以通过在“前进”和“后退”之间更改方向
参数的值来来回切换。对于计算机科学专业的学生来说,可能有一个有趣的练习来确定是否有一类XSLT转换是可逆的。这当然意味着转换必须是无损的。最明显的候选者是除了重命名元素之外什么也不做的转换,但即使这样,我认为(在不了解源文档词汇表和结构的情况下)要证明样式表不会将两个不同的输入名称映射到同一个输出名称还是相当困难的。所以我认为它最终将是一个相当小的集合
在实践中要有用的话,也要考虑添加冗余信息的转换,例如HTML报头,以及可能的转换,其中两个输入元素映射到相同的输出元素,但具有不同的属性(例如,div类=“x”,其中x允许重构输入元素名)。p> 您知道SQL Server是如何做到这一点的吗?了解:事务、事务日志、展开/中止事务。使转换成为事务系统中的事务。感谢你们的帮助,不幸的是,这似乎并没有解决我的问题,因为我希望转换的实际XSL要大得多,并将行业CDM映射到企业CDM。我真的希望有人知道一个很酷的工具可以做到这一点。user2313879,人们向你解释说,在一般情况下,逆转转换是不可能的——你还想要什么?与函数类似——只有一小部分函数是可逆的——当使用不同的参数调用时,这些函数具有不同的值。大多数函数没有此属性。转换是源XML文档和提供的任何全局参数以及全局上下文的函数。我认为最佳答案是正确的,并感谢所有人的帮助。您还希望我做什么?我所做的只是提到,我希望这是可能的,但不幸的是它不是。user2313879,这通常是不可能的-这是很高兴知道。