我需要更多帮助来遍历 XML 文档

I need more help traversing an XML document

我最近写了一些代码来遍历 XML 使用 <xsl:for-each select="//dfor:children/dfor:child">

这段代码运行良好,但我知道需要遍历相同的 child 元素,但是从指定的 parent 开始,因为现在有多个 children 集 - 它们有一个 parent “otom_businessPartner” 和 “otom_expense” 的名称,因为迭代结果适用于两个 child 集,而不是我需要的单个集。

这里是 XML:

<?xml version="1.0" encoding="UTF-8"?>
<dfor:form-data xmlns:dfor="http://kana.com/dforms">
    <dfor:field>
        <dfor:name>otom_businessPartner</dfor:name>
        <dfor:children>
            <dfor:child>
                <dfor:field>
                    <dfor:name>txt_bpName</dfor:name>
                    <dfor:value>Southampton City Council</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>txt_bpRelationship</dfor:name>
                    <dfor:value>Southampton City Council</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>txt_bpShare</dfor:name>
                    <dfor:value>Southampton City Council</dfor:value>
                </dfor:field>
            </dfor:child>
            <dfor:child>
                <dfor:field>
                    <dfor:name>txt_bpName</dfor:name>
                    <dfor:value>222222222</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>txt_bpRelationship</dfor:name>
                    <dfor:value>222222222</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>txt_bpShare</dfor:name>
                    <dfor:value>2222222222222222</dfor:value>
                </dfor:field>
            </dfor:child>
        </dfor:children>
    </dfor:field>

    <dfor:field>
        <dfor:name>otom_expenses</dfor:name>
        <dfor:children>
            <dfor:child>
                <dfor:field>
                    <dfor:name>sel_expense</dfor:name>
                    <dfor:value>Advertising</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>txt_other2</dfor:name>
                    <dfor:value/>
                </dfor:field>
                <dfor:field>
                    <dfor:name>num_actual</dfor:name>
                    <dfor:value>100</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>num_private</dfor:name>
                    <dfor:value>100</dfor:value>
                </dfor:field>
            </dfor:child>
            <dfor:child>
                <dfor:field>
                    <dfor:name>sel_expense</dfor:name>
                    <dfor:value>Leasing charges</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>txt_other2</dfor:name>
                    <dfor:value>car</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>num_actual</dfor:name>
                    <dfor:value>200</dfor:value>
                </dfor:field>
                <dfor:field>
                    <dfor:name>num_private</dfor:name>
                    <dfor:value>100</dfor:value>
                </dfor:field>
            </dfor:child>
        </dfor:children>
    </dfor:field>

</dfor:form-data>

这是 XLST:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dfor="http://kana.com/dforms" exclude-result-prefixes="dfor">

    <xsl:param name="name" /> 
    <xsl:param name="ref" /> 
    
    <xsl:template match="/">
        <html>
            <body>
                
                        <p><strong>SECTION TITLE</strong></p>

                        <xsl:for-each select="//dfor:child">
                            
                            <h4>Business partner <xsl:value-of select="position()" />:</h4>
                            <ul>
                            <xsl:for-each select="dfor:field">

                                <!-- Business Partner's name -->
                                <xsl:if test="dfor:name='txt_bpName'">
                                    <li><strong>Business Partner's name: </strong> <xsl:value-of select="dfor:value"/></li>
                                </xsl:if>

                                <!-- Business Partner's relationship -->
                                <xsl:if test="dfor:name='txt_bpRelationship'">
                                    <li><strong>Business Partner's relationship: </strong> <xsl:value-of select="dfor:value"/></li>
                                </xsl:if>

                                <!-- Share of profits -->
                                <xsl:if test="dfor:name='txt_bpShare'">
                                    <li><strong>Share of profits: </strong> <xsl:value-of select="dfor:value"/></li>
                                </xsl:if>
                                
                            </xsl:for-each>
                            </ul>
        
                        </xsl:for-each>

            </body>
        </html> 
    </xsl:template>
</xsl:stylesheet>

它return全部4children(2个来自otom_businessPartners,2个来自otom_expense)我需要找出正确的遍历备份“ otom_businessPartners" 和 "otom_expense" 只返回我想要的两个。

这是需要工作的一行行。 ' '

它需要类似于' 这显然是不正确的,因为我想 return 只是“otom_businessPartners”的 children。

因为你没有post预期的结果,我这里主要是猜测。看起来你想做类似的事情(简化的例子):

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:dfor="http://kana.com/dforms" 
exclude-result-prefixes="dfor">

<xsl:template match="/dfor:form-data">
    <html>
        <body>
            <xsl:for-each select="dfor:field[dfor:name='otom_businessPartner']/dfor:children/dfor:child">
                <h4>
                    <xsl:text>Business partner </xsl:text>
                    <xsl:value-of select="position()" />
                    <xsl:text>:</xsl:text>
                </h4>
                <ul>
                    <!-- Business Partner's name -->
                    <li>
                        <strong>Business Partner's name: </strong>
                        <xsl:value-of select="dfor:field[dfor:name='txt_bpName']/dfor:value"/>
                    </li>

                    <!-- more here -->
                    
                </ul>
            </xsl:for-each>

        </body>
    </html> 
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,这将 return:

结果

<html>
   <body>
      <h4>Business partner 1:</h4>
      <ul>
         <li><strong>Business Partner's name: </strong>Southampton City Council
         </li>
      </ul>
      <h4>Business partner 2:</h4>
      <ul>
         <li><strong>Business Partner's name: </strong>222222222
         </li>
      </ul>
   </body>
</html>

渲染: