xml - 通过匹配另一个节点名称来获取节点或通过匹配另一个节点来排除节点

标签 xml xslt transform

我有以下 XML

  <section>
            <object>
                <field name="First Source" />
                <tableSection 
                        propertyCount="1"
                        rowCount="1">
                    <tableProperty height="0"
                            width="570"
                            visible="true">
                        <property name="commit" />
                    </tableProperty>
                    <tableRow height="0"
                            width="0">
                        <tableCell value="Value First Source" />    
                    </tableRow>
                </tableSection>
            </object>
            <object>
                <field name="Another Source" />
                <tableSection 
                        propertyCount="1"
                        rowCount="1">
                    <tableProperty height="0"
                            width="570"
                            visible="true">
                        <property name="commit" />
                    </tableProperty>    
                    <tableRow height="0"
                            width="0">
                        <tableCell value="Invalid Value" />
                    </tableRow>
                </tableSection>
            </object>
        </section>

并有一个如下的 xslt

<xsl:template match="tableRow">
    <xsl:variable name="rowNodePosition">
        <xsl:value-of select="position()"/>
    </xsl:variable>
    <tr allowDblCl="true"  valign="top"  height="50px">
        <td>
            <b>Row:</b>
            <xsl:value-of select="$rowNodePosition"/>
            <br/>
            <xsl:for-each select="tableCell" >

                <xsl:variable name="currPosition">
                    <xsl:value-of select="position()"/>
                </xsl:variable>                 
                <xsl:if test="@value != ''">
                    <b>
                        <xsl:value-of select="../../tableProperty[position() = $currPosition]/property/@name"/>: </b>
                    <xsl:value-of select="@value"/>
                    <br/>
                </xsl:if>
            </xsl:for-each>
        </td>
    </tr>
    <tr>
        <td colspan="4" height="15px"> </td>
    </tr>
</xsl:template>

这将获取所有“tablRow”。但我需要排除具有字段 name='Another Source' 的 tableRows,即如果对象节点具有名称为“Another Source”的“field”,则排除节点 tableSection 的 tableRow

Node hierarchy

最佳答案

正如您所写,您希望排除每个 tableRow 元素,其中 具有特定值的相应字段名称。

如果要排除 XSLT 中的某些元素,一般规则是编写 此元素的空模板

这个元素的名称是tableRow,但是为了缩小匹配范围, 您必须添加以下谓词:

  • 向上移动 2 级(到对象级别)。
  • 下降到子字段元素。
  • 下降至其名称属性。
  • 检查其内容是否为其他来源

所以添加:

<xsl:template match="tableRow[../../field/@name = 'Another Source']"/>

到您的 XSLT 脚本,以实现您想要的。

有关工作示例,请参阅 http://xsltransform.net/ei5PwjS

关于xml - 通过匹配另一个节点名称来获取节点或通过匹配另一个节点来排除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52665495/

相关文章:

xslt - 如何使用 XSLT 将节点集放入属性值中?

jquery - CSS 过渡在 Chrome 中无法正常工作

xml - XML 是否有 "one schema rule"?

c++ 读取xml文件的内容

java - 如何在 Eclipse 中更改布局 (XML) 编辑器的字体?

CSS 动画涉及 Safari 上的不透明度、缩放和翻译问题

css - 倾斜图像的一个 Angular

java - FileInputStream.read 抛出 java.io.IOException : Result too large

xml - 如何在 XSLT 中创建键值对并使用键获取值

xslt - 如何评估 XSL 构造的 XPath 表达式