繁体   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