Xml XPath(XSLT)只匹配特定结构
如何仅匹配特定结构,如:Xml XPath(XSLT)只匹配特定结构,xml,xslt,xpath,Xml,Xslt,Xpath,如何仅匹配特定结构,如: <sandwich> <bacon /> <lettuce /> <tomato /> </sandwich> 我想用类似的东西来代替它 <sandwich> <cheese /> <bacon /> <lettuce /> <tomato /> <mayo /> <
<sandwich>
<bacon />
<lettuce />
<tomato />
</sandwich>
我想用类似的东西来代替它
<sandwich>
<cheese />
<bacon />
<lettuce />
<tomato />
<mayo />
</sandwich>
但不影响任何其他类似的“三明治”,例如:
<sandwich>
<bacon />
<egg />
</sandwich>
更具体地说,我有
<view>
<layout>
<sidebar>Some content and nodes</sidebar>
<content>Some more content and nodes</content>
</layout>
<layout>
<content>Some content and nodes</content>
<sidebar>Some more content and nodes</sidebar>
</layout>
</view>
一些内容和节点
更多内容和节点
一些内容和节点
更多内容和节点
我想把它转换成:
<view>
<foo>
<bar>Some content and nodes</bar>
<baz>Some more content and nodes</baz>
</foo>
<layout>
<content>Some content and nodes</content>
<sidebar>Some more content and nodes</sidebar>
</layout>
</view>
一些内容和节点
更多内容和节点
一些内容和节点
更多内容和节点
(使用XSLT)通过以下方法解决了此问题:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//layout[count(*)=2][sidebar/following-sibling::content]">
<foo>
<bar>
<xsl:copy-of select="sidebar/node()" />
</bar>
<baz>
<xsl:copy-of select="content/node()" />
</baz>
</foo>
</xsl:template>
</xsl:stylesheet>
你说“特定”-但你不知道是什么使一个三明治/版面“特定”,而不是另一个。@michael.hor257k版面
节点正好包含两个子节点,侧边栏
和内容
。你不需要///code>中的///code>。您在布局
上进行匹配,而不是选择它,并且匹配模式(有或没有/
)的优先级高于另一个xsl:template
@TonyGraham中匹配模式中的节点测试,即使布局
不在根目录中?(不在我的办公桌上测试)