我如何仅匹配特定结构,如:
<sandwich>
<bacon />
<lettuce />
<tomato />
</sandwich>
我想用
之类的东西替换它<sandwich>
<cheese />
<bacon />
<lettuce />
<tomato />
<mayo />
</sandwich>
但不影响任何其他类似的&#34;三明治&#34;,例如:
<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)
答案 0 :(得分:0)
管理以解决它:
<?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>