【问题标题】:Getting a node by matching another nodes name or exclude a node by matching another node通过匹配另一个节点名称来获取一个节点或通过匹配另一个节点来排除一个节点
【发布时间】:2018-10-05 12:19:32
【问题描述】:

我有以下 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'。但是我需要排除字段名称='Another Source'的tableRows,即如果对象节点具有名称为“Another Source”的'field',则排除节点tableSection的tableRow

【问题讨论】:

  • 你想递归地应用模板。创建一个只匹配你想要的对象的模板,然后告诉它应用模板来匹配 tableRows。这样 tableRow 模板不会触发那些你不想要的。

标签: xml xslt transform


【解决方案1】:

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

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

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

  • 向上移动 2 个级别(到 object 级别)。
  • 下降到子 field 元素。
  • 下降到其 name 属性。
  • 检查其内容是否为Another Source

所以添加:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多