Xslt 将属性获取为列标题
我有以下XML:Xslt 将属性获取为列标题,xslt,Xslt,我有以下XML: <DEVICEMESSAGES> <VERSION xml="1" checksum="" revision="0" envision="33050000" device="" /> <HEADER id1="0001" id2="0001" content="Nasher[<messageid>]: <!payload>" /> <MESSAGE level="7"
<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>
我正在使用以下XSLT:
<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/HEADERS">
<xsl:value-of select="@id2"/>,<xsl:text/>
<xsl:value-of select="@content"/>,<xsl:text/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
,
,
;
我得到以下输出:
0001 , Nasher[<messageid>]: <!payload>
0001,Nasher[]:
而我也需要列标题:
id2, content
0001 , Nasher[<messageid>]: <!payload>
id2,内容
0001,纳什[]:
如果设备消息
是文档元素,并且您有重复的消息
元素,那么这应该可以工作:
<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>
然后你可以用这个:
<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>
id2,内容和#xa;
,
,
;
编辑:另一种使用根节点模板匹配且不需要知道文档元素是什么的替代方法:
<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>
id2,内容和#xa;
,
,
;
如果设备消息
是文档元素,并且您有重复的消息
元素,那么这应该可以工作:
<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>
然后你可以用这个:
<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>
id2,内容和#xa;
,
,
;
编辑:另一种使用根节点模板匹配且不需要知道文档元素是什么的替代方法:
<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>
id2,内容和#xa;
,
,
;
您是否有重复的设备消息
元素、重复的标题
元素,或者两个元素都重复(尝试确定将标题锚定到什么位置)?而且,您的XML和XSLT不匹配。是标题
还是标题
?您是否有重复的设备消息
元素、重复的标题
元素,还是两者都重复(试图确定标题的定位对象)?而且,您的XML和XSLT不匹配。是标题
或标题
?或模板匹配=“*”并让该模板显式调用DEVICEMESSAGE/HEADER上的匹配。@Nat-可能/*
以限制匹配,以便不匹配文档中除文档元素以外的任何元素。或者只为根节点添加模板匹配。有很多方法可以做到这一点。它有助于查看数据结构的完整示例。或者template match=“*”并让该模板显式调用DEVICEMESSAGE/HEADER上的match。@Nat-可能/*
来限制匹配,以便不匹配文档中除document元素外的任何元素。或者只为根节点添加模板匹配。有很多方法可以做到这一点。它有助于查看数据结构的完整示例。