如何在不使用变量的情况下获取 n 元素的值?

How to fetch value of n element without using variable?

我想从元素中获取值而不将值存储在变量中。我的要求很长,所以我在这里缩短。它包含多个标签。我尝试使用 XSLT,但无法获得预期的输出。由于多个标签,我无法进入正确的路径。如何在不在 XSLT 中使用变量的情况下实现以下输出?

下面是我的xml请求:

<documents>
    <document>
    <doc1>1234</doc1>
    <doc2>4578</doc2>
   <locations>
    <location>
    <companies>
    <company>    
      <type>E-123</type>
      <empid>E-457</empid>
    <pays>
      <pay>
       <pay1>2L</pay1>
       <pay2>4L</pay2>
     </pay>
    </pays>
    <Pays>
     <pay>
      <due>2L</due>
      <month>30k</month>
     </pay>
    </pays>
    </company>
   </companies>
  </location>
    </locations>
  </document>
 </documents>

这是我的预期输出:

<empdetails>
  <details>
  <emptype>E-123</emptype>
  <ID>E-457</ID>
  <Payment>2L</Payment>
  <Payment2>4L</Payment2>
  <dueperyr>50k</dueperyr>
  <duemonth>30k</duemonth>
  </details>
  </empdetails>

我试过的 XSLT:

<xsl:stylesheet extension-element-prefixes="dp apim" version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="documents">
        <soapenv:Envelope>
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:apply-templates select="/documents/document" />
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
    <xsl:template match="companies">
    <empdetails>
      <details>
       <emptype>
        <xsl:value-of select="company/type"/>
         </emptype> 
            <ID>
            <xsl:value-of select="company/empid"/>
             </ID>
            </xsl:template>
            <xsl:template match="pays">
            <xsl:for-each select="pays/pay">
             <Payment>
              <xsl:value-of select="pay/pay1"/>
              </Payment>
               <Payment2>
                <xsl:value-of select="pay/pay2"/>
              </Payment2>
              </xsl:template>                  
               <dueperyr>
                <xsl:value-of select="pay/due"/>
                </dueperyr>     
                 <duemonth>
                  <xsl:value-of select="pay/month"/>
                 </duemonth>    
                </xsl:for-each>                
             </details>
      </empdetails> 
      </xsl:stylesheet>

你可以试试这个

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="documents">
        <soapenv:Envelope>
            <soapenv:Header/>
            <soapenv:Body>
                <xsl:apply-templates select="/documents/document" />
            </soapenv:Body>
        </soapenv:Envelope>
    </xsl:template>
    <xsl:template match="companies">
        <empdetails>
            <details>
                <emptype>
                    <xsl:value-of select="company/type"/>
                </emptype> 
                <ID>
                    <xsl:value-of select="company/empid"/>
                </ID>
                <xsl:for-each select="company/pays">
                    <xsl:apply-templates select="pay"/>
                </xsl:for-each>
            </details>
        </empdetails>
    </xsl:template>
    <xsl:template match="pay1">
        <Payment>
            <xsl:apply-templates/>
        </Payment>
    </xsl:template>
    
    <xsl:template match="pay2">
        <Payment2>
            <xsl:apply-templates/>
        </Payment2>
    </xsl:template>
    
    <xsl:template match="due">
        <dueperyr>
            <xsl:apply-templates/>
        </dueperyr>
    </xsl:template>
    
    <xsl:template match="month">
        <duemonth>
            <xsl:apply-templates/>
        </duemonth>
    </xsl:template>
</xsl:stylesheet>

link: https://xsltfiddle.liberty-development.net/eieFA1G