如何用XSLT提取<Section name="Cechy">内容并转换为表格式<description>
解决方案
假设你的输入XML结构类似这样(如果实际结构不同,可根据说明调整):
<Root> <Section name="Cechy"> <Attribute name="Rozmiar" value="XL"/> <Attribute name="Kolor" value="Niebieski"/> <Attribute name="Materiał" value="Bawełna"/> </Section> </Root>
以下是完成需求的XSLT代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <!-- 匹配根节点,触发目标Section的处理 --> <xsl:template match="/"> <xsl:apply-templates select="//Section[@name='Cechy']"/> </xsl:template> <!-- 生成包含HTML表格的description元素 --> <xsl:template match="Section[@name='Cechy']"> <description> <table border="1"> <thead> <tr> <th>Nazwa właściwości</th> <th>Wartość</th> </tr> </thead> <tbody> <!-- 遍历Section下的所有子节点,替换*为实际节点名(比如Property)如果需要 --> <xsl:for-each select="*"> <tr> <td><xsl:value-of select="@name"/></td> <td><xsl:value-of select="@value"/></td> </tr> </xsl:for-each> </tbody> </table> </description> </xsl:template> <!-- 忽略不需要处理的节点和文本 --> <xsl:template match="text()|@*"/> </xsl:stylesheet>
关键说明:
- 用
//Section[@name='Cechy']精准定位到你需要处理的Section节点 - 表格的表头文字可根据需求自行修改
- 如果Section下的子节点不是
<Attribute>,把select="*"换成实际的节点名称(比如select="Property") - 如果子节点的属性名不是
name和value,调整xsl:value-of的路径即可(比如子元素是<Name>和<Value>,就改成select="Name"和select="Value")
另外,感谢之前帮你构建代码的伙伴!
内容的提问来源于stack exchange,提问作者user20508390
相关产品推荐
相关产品推荐

