【问题标题】:for each loop in XSLT对于 XSLT 中的每个循环
【发布时间】:2016-03-29 11:53:54
【问题描述】:

我想使用 XSLT 进行一次转换。根据下面的示例,有多个 EmpRecord 可用,每个 EmpRecord 包含 EmpDept 和该部门下可用员工的 Profile。

XML:

<Employee>
    <EmpRecord>
        <EmpDept>Accounting</EmpDept>
        <EmpData>
            <Name>Joy</Name>
            <Age>32</Age>
        </EmpData>
    </EmpRecord>
    <EmpRecord>
        <EmpDept>Finance</EmpDept>
    </EmpRecord>
    <EmpRecord>
        <EmpDept>IT</EmpDept>
        <EmpData>
            <Name>Sam</Name>
            <Age>27</Age>
        </EmpData>
        <EmpData>
            <Name>John</Name>
            <Age>25</Age>
        </EmpData>
        <EmpData>
            <Name>Ricky</Name>
            <Age>31</Age>
        </EmpData>
    </EmpRecord>
</Employee>

预期输出:

<Employee>
    <EmpRecord>
        <Department>Accounting</Department>
        <EmpData>
            <EmpName>Joy</EmpName>
            <Age>32</Age>
        </EmpData>
    </EmpRecord>
    <EmpRecord>
        <Department>IT</Department>
        <EmpData>
            <EmpName>Sam</EmpName>
            <Age>27</Age>
        </EmpData>
        <EmpData>
            <EmpName>John</EmpName>
            <Age>25</Age>
        </EmpData>
        <EmpData>
            <EmpName>Ricky</EmpName>
            <Age>31</Age>
        </EmpData>
    </EmpRecord>
</Employee>

我能够获取所有可用的 EmpData,但无法获取 EmpDept。

使用的 XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Employee>
<xsl:for-each select="/Employee/EmpRecord/EmpData">
<Department><xsl:value-of select="./EmpDept"/></Department>
<EmpData>
<EmpName><xsl:value-of select="./Name"/></EmpName>
<Age><xsl:value-of select="./Age"/></Age>
</EmpData>
</xsl:for-each>
</Employee>

</xsl:template>
</xsl:stylesheet>

所以我只想要那些至少有一名员工可用的 EmpDept。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    试试这个方法:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="/Employee">
        <Employee>
            <xsl:for-each select="EmpRecord[EmpData]">
                <EmpRecord>
                    <Department>
                        <xsl:value-of select="EmpDept"/>
                    </Department>
                    <xsl:for-each select="EmpData">
                        <EmpData>
                            <EmpName>
                                <xsl:value-of select="Name"/>
                            </EmpName>
                            <xsl:copy-of select="Age"/>
                        </EmpData>
                    </xsl:for-each>
                </EmpRecord>
            </xsl:for-each>
        </Employee>
    </xsl:template>
    
    </xsl:stylesheet>
    

    或者,如果您愿意:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="EmpRecord[not(EmpData)]"/>
    
    <xsl:template match="EmpDept">
        <Department>
            <xsl:apply-templates/>
        </Department>
    </xsl:template>
    
    <xsl:template match="Name">
        <EmpName>
            <xsl:apply-templates/>
        </EmpName>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案2】:

      您真正想做的是:将所有内容从源 xml 复制到目标 xml,除了 EmpRecord 不包含任何 EmpData。对?试试这个:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:output encoding="UTF-8" method="xml" version="1.0" indent="yes"/>
          <!-- identity template -->
          <xsl:template match="@*|node()">
              <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
              </xsl:copy>
          </xsl:template>
          <!-- specific part -->
          <xsl:template match="EmpRecord[not(EmpData)]"/>
      </xsl:stylesheet>
      

      “身份模板”复制所有内容,“特定部分”删除不需要的元素。

      【讨论】:

      • 对不起,如果我的预期输出具有误导性,实际上我不想完全复制。在我的实际工作中需要改变标签和结构。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2014-05-04
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多