在 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
谁能指导我如何计算许可证的 for-each 值?
这应该可以解决您的问题:
<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> </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我在这里所做的是将许可证的 position
保存在一个变量中,而不是在检查父代码时,确保它具有相同的位置。
此代码呈现请求的结果:
<?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> </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],' ')"/>
</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。
我是 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
谁能指导我如何计算许可证的 for-each 值?
这应该可以解决您的问题:
<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> </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
我在这里所做的是将许可证的 position
保存在一个变量中,而不是在检查父代码时,确保它具有相同的位置。
此代码呈现请求的结果:
<?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> </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],' ')"/>
</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。