簡體   English   中英

如何在“for-each 循環”中反擊索引

[英]How to counter-step index inside “for-each loop”

我正在尋找一種 XSLT 處理 XML 節點“SalesInternalData”的方法。 澄清一下,根據當前代碼,XSL 正在做它預期做的事情。 出於測試目的,我已對索引號進行了硬編碼。 在 /SalesInternalData[1] 和 /SalesInternalData[2] 之間手動交換會顯示正確的數據。

我保留了節點重命名和千位分隔符,因為兩者都需要留在解決方案中。

問題:

有沒有辦法重用我的代碼庫但確保 XSL 迭代 /SalesInternalData[1] 和 /SalesInternalData[2]? 請注意,每次“for-each”迭代時索引號存在兩次。

找到解決方案會使我的 XSL 代碼庫減少 50%。

下面的代碼可以在這里執行: https://xsltfiddle.liberty-development.net/pNEj9cU/1

XML 文件:

<?xml version="1.0" encoding="utf-8" ?>

<accounting>
    <SalesInternalData period="balance0">1000</SalesInternalData>
    <SalesInternalData period="balance1">2000</SalesInternalData>
</accounting>

XSL 文件:

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
xmlns="http://www.example.org/0"
xmlns:ix="http://www.example.org/1"
>
  
<xsl:output method="html" indent="yes" html-version="5"/>  

<!-- Decimal formatting -->
<xsl:decimal-format name="thousand-delimiter" grouping-separator=" "/>


  <xsl:template match="/accounting">
  
  <html>
  
  <xsl:for-each select="/accounting/SalesInternalData">

      <td>
        <span>
          <ix:SalesExternalData>
            
            <!-- Extract all attributes and it's values-->
            <xsl:copy-of select="/accounting/SalesInternalData[1]/@*"/>
            
            <!-- Set thousand delimiter to space-->
            <xsl:value-of select="
            format-number(
                /accounting/SalesInternalData[1],
                '# ###', 
                'thousand-delimiter'
            )"/>
            
          </ix:SalesExternalData>
        </span>
      </td>

    </xsl:for-each>
    
</html>
  
  </xsl:template>
  
</xsl:stylesheet>

結果(預期為當前代碼):

<!DOCTYPE HTML>
<html xmlns="http://www.example.org/0" xmlns:ix="http://www.example.org/1">
   <td><span>
         <ix:SalesExternalData period="balance0">1 000</ix:SalesExternalData></span></td>
   <td><span>
         <ix:SalesExternalData period="balance0">1 000</ix:SalesExternalData></span></td>
</html>

想要的結果:

<!DOCTYPE HTML>
<html xmlns="http://www.example.org/0" xmlns:ix="http://www.example.org/1">
   <td><span>
         <ix:SalesExternalData period="balance0">1 000</ix:SalesExternalData></span></td>
   <td><span>
         <ix:SalesExternalData period="balance1">2 000</ix:SalesExternalData></span></td>
</html>

我正在尋找一種 XSLT 處理 XML 節點“SalesInternalData”的方法。 澄清一下,根據當前代碼,XSL 正在做它預期做的事情。 出於測試目的,我已對索引號進行了硬編碼。 在 /SalesInternalData[1] 和 /SalesInternalData[2] 之間手動交換會顯示正確的數據。

我保留了節點重命名和千位分隔符,因為兩者都需要留在解決方案中。

問題:

有沒有辦法重用我的代碼庫但確保 XSL 迭代 /SalesInternalData[1] 和 /SalesInternalData[2]? 請注意,每次“for-each”迭代時索引號存在兩次。

找到解決方案會使我的 XSL 代碼庫減少 50%。

下面的代碼可以在這里執行: https://xsltfiddle.liberty-development.net/pNEj9cU/1

XML 文件:

<?xml version="1.0" encoding="utf-8" ?>

<accounting>
    <SalesInternalData period="balance0">1000</SalesInternalData>
    <SalesInternalData period="balance1">2000</SalesInternalData>
</accounting>

XSL 文件:

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
xmlns="http://www.example.org/0"
xmlns:ix="http://www.example.org/1"
>
  
<xsl:output method="html" indent="yes" html-version="5"/>  

<!-- Decimal formatting -->
<xsl:decimal-format name="thousand-delimiter" grouping-separator=" "/>


  <xsl:template match="/accounting">
  
  <html>
  
  <xsl:for-each select="/accounting/SalesInternalData">

      <td>
        <span>
          <ix:SalesExternalData>
            
            <!-- Extract all attributes and it's values-->
            <xsl:copy-of select="/accounting/SalesInternalData[1]/@*"/>
            
            <!-- Set thousand delimiter to space-->
            <xsl:value-of select="
            format-number(
                /accounting/SalesInternalData[1],
                '# ###', 
                'thousand-delimiter'
            )"/>
            
          </ix:SalesExternalData>
        </span>
      </td>

    </xsl:for-each>
    
</html>
  
  </xsl:template>
  
</xsl:stylesheet>

結果(預期為當前代碼):

<!DOCTYPE HTML>
<html xmlns="http://www.example.org/0" xmlns:ix="http://www.example.org/1">
   <td><span>
         <ix:SalesExternalData period="balance0">1 000</ix:SalesExternalData></span></td>
   <td><span>
         <ix:SalesExternalData period="balance0">1 000</ix:SalesExternalData></span></td>
</html>

想要的結果:

<!DOCTYPE HTML>
<html xmlns="http://www.example.org/0" xmlns:ix="http://www.example.org/1">
   <td><span>
         <ix:SalesExternalData period="balance0">1 000</ix:SalesExternalData></span></td>
   <td><span>
         <ix:SalesExternalData period="balance1">2 000</ix:SalesExternalData></span></td>
</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM