簡體   English   中英

還有另一種方法嗎? XSLT嵌套每個

[英]There is another way to do this? XSLT nested for-each

我的問題是關於如何在n個日期和“ fechas”(不允許使用另一個模板)中執行此操作。 我很難做到,我對“ for-each”功能很陌生。 謝謝!

輸入xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transformer.xsl"?>
<dc>
   <date>11</date>
   <date>20</date>
   <status>Available</status>
   <status>Collected</status>
</dc>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="/dc/status">
    <date>
        <xsl:if test="position()=1">
            <xsl:attribute name="fechas">
                <xsl:value-of select="."/>
            </xsl:attribute>
        <xsl:for-each select="/dc/date">
            <xsl:if test="position()=1">
                <xsl:value-of select="."/>
            </xsl:if>
        </xsl:for-each>    
        </xsl:if>
        <xsl:if test="position()=2">
            <xsl:attribute name="fechas">
                <xsl:value-of select="."/>
            </xsl:attribute>
            <xsl:for-each select="/dc/date">
                <xsl:if test="position()=2">
                    <xsl:value-of select="."/>
                </xsl:if>
            </xsl:for-each>    
        </xsl:if>
    </date>
</xsl:for-each>

這是我的輸出:

<?xml version="1.0" encoding="UTF-8"?>
 <date fechas="Available">11</date>
 <date fechas="Collected">20</date>

您可以嘗試以下方法:

<xsl:template match="/">
    <xsl:for-each select="dc/date">
        <xsl:variable name="pos" select="position()"/>
        <xsl:copy>
            <xsl:if test="../status[$pos]">
                <xsl:attribute name="fechas">
                    <xsl:value-of select="../status[$pos]"/>
                </xsl:attribute>
            </xsl:if>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:for-each>
</xsl:template>

參見http://xsltransform.net/pNvs5w4上的 Transformation

這有什么問題:

<xsl:template match="/">
  <date fechas="{dc/status[1]}>
    <xsl:value-of select="dc/date[1]"/>
  </date>
  <date fechas="{dc/status[2]}>
    <xsl:value-of select="dc/date[2]"/>
  </date>
</xsl:template>

暫無
暫無

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

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