簡體   English   中英

XSLT 1.0 - 帶有選擇變量的 for-each 沒有給出正確的結果

[英]XSLT 1.0 - for-each with a choose variable not giving the right results

輸入:

   <?xml version="1.0" encoding="UTF-8"?>
      <Project ID="123">
        <ProductPool>
          <Product Type="A" ID="123" DueDate="123" Name="ABC"/>
          <Product Type="B" ID="123" DueDate="123" Name="ABC"/>
          <Product Type="A" ID="123" DueDate="123" Name="ABC"/>
          <Product Type="B" ID="123" DueDate="123" Name="ABC"/>
          <Product Type="A" ID="123" DueDate="123" Name="ABC"/>   
        </ProductPool>
      </Project>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:element name="Product">
            <xsl:attribute name="ID">
                <xsl:value-of select="//Project/@ID"/>
            </xsl:attribute>
                <xsl:variable name="elem-name">
                    <xsl:choose>
                        <xsl:when test='//Product[@Type="A"]'>BoundComponent</xsl:when>
                        <xsl:otherwise>UnboundComponent</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:for-each select="//Product">
                <xsl:element name="{$elem-name}">
                    <xsl:attribute name="ID">
                        <xsl:value-of select="@Name"/>
                    </xsl:attribute>
                    <xsl:attribute name="DueDate">
                        <xsl:value-of select="@DueDate"/>
                    </xsl:attribute>
                </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

輸出:

<?xml version="1.0"?>
<Product ID="123">
  <BoundComponent ID="ABC" Duedate="123"/>
  <BoundComponent ID="ABC" Duedate="123"/>
  <BoundComponent ID="ABC" Duedate="123"/>
  <BoundComponent ID="ABC" Duedate="123"/>
  <BoundComponent ID="ABC" Duedate="123"/>
</Product>

出於某種原因,一旦在整個輸入中找到一個 Type="A",所有元素都稱為 BoundComponent,但我的目標是分離輸出,因此找到的每個產品要么是一個名為 Bound 或 Unbound 的元素,具體取決於關於它在輸入中的類型,我不知道為什么會這樣,因為它已經是一個 for-each 循環。 有任何想法嗎?

您的$elem-name變量被綁定到循環外的值。 如果將xsl:variable放在循環中,它將綁定到每個不同Product的適當值。 IE

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:element name="Product">
            <xsl:attribute name="ID">
                <xsl:value-of select="//Project/@ID"/>
            </xsl:attribute>
            <xsl:for-each select="//Product">
                <xsl:variable name="elem-name">
                    <xsl:choose>
                        <xsl:when test='@Type="A"'>BoundComponent</xsl:when>
                        <xsl:otherwise>UnboundComponent</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:element name="{$elem-name}">
                    <xsl:attribute name="ID">
                        <xsl:value-of select="@Name"/>
                    </xsl:attribute>
                    <xsl:attribute name="DueDate">
                        <xsl:value-of select="@DueDate"/>
                    </xsl:attribute>
                </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

暫無
暫無

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

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