<start>
<tag> dog=German shepherd|cat=has a lot of spots </tag>
</start>
<start>
<tag> cat=black cat|dog=Clifford </tag>
</start>
假设我有一个像这样的XML文档,并希望循环遍历它并选择cat和dog的值并使用XSL单独打印它们应该如何进行呢?
答案 0 :(得分:0)
给定格式良好的输入XML,例如:
<root>
<start>
<tag> dog=German shepherd|cat=has a lot of spots </tag>
</start>
<start>
<tag> cat=black cat|dog=Clifford </tag>
</start>
</root>
和XSLT 2.0处理器,您可以应用以下样式表:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="start/tokenize(tag, '\|')">
<entry>
<key>
<xsl:value-of select="substring-before(., '=')" />
</key>
<value>
<xsl:value-of select="substring-after(., '=')" />
</value>
</entry>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
接收:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<entry>
<key> dog</key>
<value>German shepherd</value>
</entry>
<entry>
<key>cat</key>
<value>has a lot of spots </value>
</entry>
<entry>
<key> cat</key>
<value>black cat</value>
</entry>
<entry>
<key>dog</key>
<value>Clifford </value>
</entry>
</root>