如果DEVICEMESSAGES
是文档元素,你都重复MESSAGE
元素,那么这应该工作:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="DEVICEMESSAGES">
<xsl:text>id2,content,
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="DEVICEMESSAGES/HEADER">
<xsl:value-of select="@id2"/>,<xsl:text/>
<xsl:value-of select="@content"/>,<xsl:text/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
如果你有不同的文档元素,然后调整模板来匹配上。
例如,如果在文档元素是doc
,你有1至N号DEVICEMESSAGES元素:
<doc>
<DEVICEMESSAGES>
<VERSION xml="1" checksum="" revision="0" envision="33050000" device="" />
<HEADER id1="0001" id2="0001" content="Nasher[<messageid>]: <!payload>" />
<MESSAGE level="7" parse="1" parsedefvalue="1" tableid="15" id1="24682" id2="24682" eventcategory="1003010000" content="Access to <webpage> was blocked due to its category (<info> by <hostname>)" />
</DEVICEMESSAGES>
<DEVICEMESSAGES>
<VERSION xml="1" checksum="" revision="0" envision="33050000" device="" />
<HEADER id1="0002" id2="0002" content="Nasher[<messageid>]: <!payload>" />
<MESSAGE level="7" parse="1" parsedefvalue="1" tableid="15" id1="24682" id2="24682" eventcategory="1003010000" content="Access to <webpage> was blocked due to its category (<info> by <hostname>)" />
</DEVICEMESSAGES>
</doc>
,那么你可以使用这个:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="doc">
<xsl:text>id2,content,
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="DEVICEMESSAGES/HEADER">
<xsl:value-of select="@id2"/>,<xsl:text/>
<xsl:value-of select="@content"/>,<xsl:text/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
编辑:另一种使用根节点模板匹配的替代方法,不需要知道文档元素是什么:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:text>id2,content,
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="DEVICEMESSAGES/HEADER">
<xsl:value-of select="@id2"/>,<xsl:text/>
<xsl:value-of select="@content"/>,<xsl:text/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
您是否重复'DEVICEMESSAGE'元素,重复'HEADER'元素,或者两者都重复(尝试确定锚定标题的内容)?另外,您的XML和XSLT不匹配。是“HEADER”还是“HEADERS”? – 2010-04-09 23:58:57