我有以下 XML:
<record>
<d989 p="Apples" t="Apples"/>
<d990 p="Oranges" t="Bananas"/>
<record_group_1>
<d991 p="Mouse" t="Mouse and Cat"/>
<d991 p="Dog" t="Dog and Cat"/>
</record_group_1>
<record_group_2>
<d992 />
</record_group_2>
...
我在确定节点是否有子节点后使用以下 XSL 模板:
<xsl:template name="hasChildren">
<tr>
<td colspan="2" class="sectionTitle">
<xsl:value-of select="translate(local-name(), '_', ' ')" />
</td>
</tr>
...
我将如何包装 <xsl:template name="hasChildren">
contents 来确定所讨论的节点是否有子节点,任何子节点都具有 p 的属性。
我正在测试当前节点是否具有 p 属性 <xsl:if test="@p">
但我不确定如何找到节点的子节点是否有 p。
对于上面的 XML 示例,我想忽略 <record_group_2>
因为它的 child 不包含 p 的属性,其中 <record_group_1>
我想处理。
如果您需要更多说明,请告诉我...
最佳答案
and I'm using the following XSL template after determining if the node has children:
<xsl:template name="hasChildren">
<tr>
<td colspan="2" class="sectionTitle">
<xsl:value-of select="translate(local-name(), '_', ' ')" />
</td>
</tr> ...
</xsl:template>
这段代码根本没有按照它所说的去做!
I'm testing if the current node has an attribute of `p` with `<xsl:if test="@p">` but I'm not sure how I could find if the node's children has a `p`.
使用:
*[@p]
此 XPath 表达式选择具有属性 p
的任何子元素(当前节点的) .在 test
中使用时<xsl:if>
的属性或 <xsl:when>
选择的节点集被转换为 bool 值:true()
如果节点集非空且 false()
否则。
For the XML example above I would want to ignore
<record_group_2>
because it's children do not contain the attribute of p where as<record_group_1>
I would want to process
使用:就是上面的表达式。
这是一个完整且非常简单且简短的转换,它仅将顶部元素的子元素复制到输出,这些子元素具有 p
。属性:
<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="/*">
<xsl:copy>
<xsl:copy-of select="*[*/@p]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
当此转换应用于提供的 XML 文档时(更正为格式正确):
<record>
<d989 p="Apples" t="Apples"/>
<d990 p="Oranges" t="Bananas"/>
<record_group_1>
<d991 p="Mouse" t="Mouse and Cat"/>
<d991 p="Dog" t="Dog and Cat"/>
</record_group_1>
<record_group_2>
<d992 />
</record_group_2>
</record>
想要的、正确的结果(没有至少一个子元素具有 p
属性的非顶级元素被删除)产生:
<record>
<record_group_1>
<d991 p="Mouse" t="Mouse and Cat"/>
<d991 p="Dog" t="Dog and Cat"/>
</record_group_1>
</record>
关于xml - XSL 检查以查看是否有任何子节点具有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9039834/