簡體   English   中英

在XML上應用XSLT,以基於當前for-each循環計數獲得祖先值

[英]Apply XSLT on XML get the ancestor value based on current for-each loop count

我是XML和XSLT的新手。 我正在嘗試通過應用XSLT生成CSV文件。

這是我的XML文件:

<people>
<person>
    <codes>
        <code>53-8907</code>
    </codes>
    <first-name>Matt</first-name>
    <licenses>
        <license>
            <state>TX</state>
        </license>
    </licenses>
</person>
<person>
    <codes>
        <code>66-8907</code>
    </codes>
    <first-name>Mike</first-name>
    <licenses>
        <license>
            <state>NY</state>
        </license>
    </licenses>
</person>
<person>
    <codes>
        <code>53-8907</code>
        <code>66-8907</code>
    </codes>
    <first-name>Rob</first-name>
    <licenses>
        <license>
            <state>TX</state>
        </license>
        <license>
            <state>NY</state>
        </license>  
    </licenses>
</person>
</people>

這是我正在使用的XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" method="text"/>
<xsl:template match="/people">
    <xsl:for-each select="person/licenses/license">
        <xsl:value-of select="ancestor::person/codes/code"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="ancestor::person/first-name"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="state"/>
        <xsl:text>
</xsl:text>
    </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

這是我想要的輸出:

53-8907,Matt,TX
66-8907,Mike,NY
53-8907,Rob,TX
66-8907,Rob,NY

這就是我得到的:

 53-8907,Matt,TX
 66-8907,Mike,NY
 53-8907,Rob,TX
 53-8907,Rob,NY

有人可以指導我如何計算許可證的每個價值嗎?

此代碼呈現請求的結果:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output encoding="UTF-8" method="text"/>
   <xsl:template match="/people">
      <xsl:for-each select="person/licenses">
         <xsl:for-each select="license">
            <xsl:variable name="Pos" select="position()"/>
            <xsl:value-of select="ancestor::person/codes/code[position() = $Pos]"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="ancestor::person/first-name"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="state"/>
            <xsl:text>&#13;</xsl:text>
         </xsl:for-each>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

另一種解決方案,將內容存儲在變量中並使用concat()

樣式表

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output encoding="UTF-8" method="text"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="person">

        <xsl:variable name="first-name" select="first-name"/>
        <xsl:variable name="licenses" select="licenses/license"/>

        <xsl:for-each select="codes/code">
            <xsl:variable name="pos" select="position()"/>
             <xsl:value-of select="concat(.,',',$first-name,',',$licenses[$pos],'&#10;')"/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

文字輸出

53-8907,Matt,TX
66-8907,Mike,NY
53-8907,Rob,TX
66-8907,Rob,NY

在此處在線嘗試: http : //xsltransform.net/94hvTze/3

這應該可以解決您的問題:

<xsl:stylesheet version="1.0" xmlns:csv="csv:csv" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" method="text"/>
    <xsl:template match="/people">
        <xsl:for-each select="person/licenses">
            <xsl:for-each select="license">
                <xsl:variable name="licensePos" select="position()"/>
                <xsl:value-of select="ancestor::person/codes/code[position()=$licensePos]"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="ancestor::person/first-name"/>
                <xsl:text>,</xsl:text>
                <xsl:value-of select="state"/>
                <xsl:text>&#13;</xsl:text>
            </xsl:for-each>
        </xsl:for-each>
     </xsl:template>
</xsl:stylesheet>

我在這里所做的是將許可證的position保留在變量中,然后在檢查父代碼時確保其位置相同。

實際觀看: http : //xsltransform.net/94hvTze/1

暫無
暫無

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

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