关于XSLT的两个问题
现在我有两个关于xslt的问题 首先,我想描述所有没有任何属性的元素。像关于XSLT的两个问题,xslt,Xslt,现在我有两个关于xslt的问题 首先,我想描述所有没有任何属性的元素。像 <element id="p0" attribute="a1"/> <element id="p1"/> <element id="p2"/> 错: t11t22t33 我应该写哪个XSLT单词?非常感谢 xsl:template中的所有相对路径都根据模板匹配的节点进行计算。测试当前节点的子节点element是否符合您的条件也是如此。如果此测试位于与元素节点匹配的模板中,那么它将不起
<element id="p0" attribute="a1"/>
<element id="p1"/>
<element id="p2"/>
错:
t11t22t33
我应该写哪个XSLT单词?非常感谢
xsl:template中的所有相对路径都根据模板匹配的节点进行计算。测试当前节点的子节点element是否符合您的条件也是如此。如果此测试位于与元素节点匹配的模板中,那么它将不起作用。你应该使用。以引用当前节点
布尔表达式元素[@attribute]=正在查找具有名为attribute的属性且自身为空的元素节点。它不测试属性内容的任何内容
基本上,您需要了解以下模板:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="element">
<xsl:text>element position=[</xsl:text>
<xsl:value-of select="count(preceding-sibling::element) + 1"/>
<xsl:text>] / </xsl:text>
<xsl:choose>
<xsl:when test=".[not(@attribute)]">
<xsl:text>no @attribute</xsl:text>
</xsl:when>
<xsl:when test=".[@attribute='']">
<xsl:text>empty @attribute</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>non-empty @attribute</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
适用于本测试文件:
<root>
<element id="p0" attribute="a1">one</element>
<element id="p1" attribute="test"></element>
<element id="p1" attribute=""></element>
<element id="p2">three</element>
</root>
element position=[1] / non-empty @attribute
element position=[2] / non-empty @attribute
element position=[3] / empty @attribute
element position=[4] / no @attribute
此模板还回答了您关于换行符的第二个问题。使用此样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="no" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:for-each select="root/element[not(@attribute='a1')]">
<xsl:value-of select="@id"/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
是否要获取属性名称属性的所有值?所有这些值都应该在换行符上输出?不。我想选择所有没有任何属性的元素。输出结果,我只是不想让它们在一行中。t11,t22和t33的值从哪里来?也许最好提供一个完整的输入XMLhey标记,t11 t22 t33只是举个例子。它们是输出值。所以我想问哪个xslt单词或句子是有用的,因为它们位于不同的行中?非常感谢你。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="no" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:for-each select="root/element[not(@attribute='a1')]">
<xsl:value-of select="@id"/><xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<root>
<element id="p0" attribute="a1"/>
<element id="p1"/>
<element id="p2"/>
</root>
p1
p2