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

Posted

技术标签:

【中文标题】我需要更多帮助来遍历 XML 文档【英文标题】:I need more help traversing an XML document 【发布时间】:2021-12-18 00:58:15 【问题描述】:

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

此代码运行良好,但我知道需要遍历相同的子元素但来自指定的父元素,因为现在有多个子集 - 它们的父名称为“otom_businessPartner”和“otom_expense”,因为迭代结果适用于两个子套,而不是我需要的一套。

这是 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>

它返回所有 4 个孩子(2 个来自 otom_businessPartners,2 个来自 otom_expense)我需要找出正确的遍历备份“otom_businessPartners”和“otom_expense”,以仅退休我想要的两个。

这是需要工作的行。 ' '

它需要类似于 ' 这显然不正确,因为我只想返回 " otom_businessPartners”。

【问题讨论】:

【参考方案1】:

由于您没有发布预期的结果,我主要是在这里猜测。看来您想做类似(简化示例)的事情:

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>

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

结果

<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>

渲染:

【讨论】:

以上是关于我需要更多帮助来遍历 XML 文档的主要内容,如果未能解决你的问题,请参考以下文章

需要示例代码片段帮助

需要帮助获取子节点

笔记:XML-解析文档-XPath 定位信息

我有一个需要解密的 xml 代码

使用 MSXML 在 Xml 中遍历节点

学习笔记-模块之xml文件处理