XSLT - 从 for-each 循环中的两个对象获取数据
XSLT - get data from two objects in for-each loop
我对 xsl 转换有疑问。这是我的代码片段。 input.xml
<models>
<model>
<contact>
<city>Tokyo</city>
<telephone>555-888-999</telephone>
</contact>
<person>
<name>Anna</name>
<surname>Smith</surname>
<age>33</age>
</person>
<person>
<name>Melissa</name>
<surname>MacBeth</surname>
<age>26</age>
</person>
</model>
<model>
<contact>
<city>New York</city>
<telephone>987-254-845</telephone>
</contact>
<person>
<name>Michael</name>
<surname>Affronti</surname>
<age>49</age>
</person>
<person>
<name>Arthur</name>
<surname>Bertrand</surname>
<age>38</age>
</person>
<person>
<name>Simon</name>
<surname>Morris</surname>
<age>22</age>
</person>
</model>
</models>
我需要将 <contact>
的模板应用到 for-each 循环中。这是必需的。
xsl_template.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="/models/model">
<xsl:apply-templates select="." />
</xsl:for-each>
</xsl:template>
<xsl:template match="contact">
<phoneNo>
<xsl:value-of select="city" />
</phoneNo>
</xsl:template>
<xsl:template match="model">
<xsl:for-each select="person">
<responsible>
<xsl:apply-templates select="contact" />
<xsl:apply-templates select="." />
</responsible>
</xsl:for-each>
</xsl:template>
<xsl:template match="person">
<person>
<xsl:value-of select="name" />
<xsl:text>, </xsl:text>
<xsl:value-of select="surname" />
</person>
</xsl:template>
</xsl:stylesheet>
output.xml
<?xml version="1.0" encoding="UTF-8"?>
<responsible>
<person>Anna, Smith</person>
</responsible>
<responsible>
<person>Melissa, MacBeth</person>
</responsible>
<responsible>
<person>Michael, Affronti</person>
</responsible>
<responsible>
<person>Arthur, Bertrand</person>
</responsible>
<responsible>
<person>Simon, Morris</person>
</responsible>
缺少 <contact>
的模板。我也尝试过使用 <xsl:variable>
,但它不起作用。我怎么才能得到它?
如果您需要显示人员的联系方式(模型中的所有人员都一样),那么:
<xsl:template match="model">
<xsl:for-each select="person">
<responsible>
<xsl:apply-templates select="preceding-sibling::contact" />
<xsl:apply-templates select="." />
</responsible>
</xsl:for-each>
</xsl:template>
你需要调整两件事,
<xsl:template match="contact">
<phoneNo>
<xsl:value-of select="city" />
</phoneNo>
</xsl:template>
s/b
<xsl:template match="contact">
<phoneNo>
<xsl:value-of select="telephone" />
</phoneNo>
</xsl:template>
和
<xsl:template match="model">
<xsl:for-each select="person">
<responsible>
<xsl:apply-templates select="contact" />
<xsl:apply-templates select="." />
</responsible>
</xsl:for-each>
</xsl:template>
s/b
<xsl:template match="model">
<xsl:for-each select="person">
<responsible>
<xsl:apply-templates select="../contact" />
<xsl:apply-templates select="." />
</responsible>
</xsl:for-each>
</xsl:template>
在xsl:for-each
循环下,你在上下文节点person
下,所以你必须使用点符号(..)来引用父节点。
我对 xsl 转换有疑问。这是我的代码片段。 input.xml
<models>
<model>
<contact>
<city>Tokyo</city>
<telephone>555-888-999</telephone>
</contact>
<person>
<name>Anna</name>
<surname>Smith</surname>
<age>33</age>
</person>
<person>
<name>Melissa</name>
<surname>MacBeth</surname>
<age>26</age>
</person>
</model>
<model>
<contact>
<city>New York</city>
<telephone>987-254-845</telephone>
</contact>
<person>
<name>Michael</name>
<surname>Affronti</surname>
<age>49</age>
</person>
<person>
<name>Arthur</name>
<surname>Bertrand</surname>
<age>38</age>
</person>
<person>
<name>Simon</name>
<surname>Morris</surname>
<age>22</age>
</person>
</model>
</models>
我需要将 <contact>
的模板应用到 for-each 循环中。这是必需的。
xsl_template.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="/models/model">
<xsl:apply-templates select="." />
</xsl:for-each>
</xsl:template>
<xsl:template match="contact">
<phoneNo>
<xsl:value-of select="city" />
</phoneNo>
</xsl:template>
<xsl:template match="model">
<xsl:for-each select="person">
<responsible>
<xsl:apply-templates select="contact" />
<xsl:apply-templates select="." />
</responsible>
</xsl:for-each>
</xsl:template>
<xsl:template match="person">
<person>
<xsl:value-of select="name" />
<xsl:text>, </xsl:text>
<xsl:value-of select="surname" />
</person>
</xsl:template>
</xsl:stylesheet>
output.xml
<?xml version="1.0" encoding="UTF-8"?>
<responsible>
<person>Anna, Smith</person>
</responsible>
<responsible>
<person>Melissa, MacBeth</person>
</responsible>
<responsible>
<person>Michael, Affronti</person>
</responsible>
<responsible>
<person>Arthur, Bertrand</person>
</responsible>
<responsible>
<person>Simon, Morris</person>
</responsible>
缺少 <contact>
的模板。我也尝试过使用 <xsl:variable>
,但它不起作用。我怎么才能得到它?
如果您需要显示人员的联系方式(模型中的所有人员都一样),那么:
<xsl:template match="model">
<xsl:for-each select="person">
<responsible>
<xsl:apply-templates select="preceding-sibling::contact" />
<xsl:apply-templates select="." />
</responsible>
</xsl:for-each>
</xsl:template>
你需要调整两件事,
<xsl:template match="contact">
<phoneNo>
<xsl:value-of select="city" />
</phoneNo>
</xsl:template>
s/b
<xsl:template match="contact">
<phoneNo>
<xsl:value-of select="telephone" />
</phoneNo>
</xsl:template>
和
<xsl:template match="model">
<xsl:for-each select="person">
<responsible>
<xsl:apply-templates select="contact" />
<xsl:apply-templates select="." />
</responsible>
</xsl:for-each>
</xsl:template>
s/b
<xsl:template match="model">
<xsl:for-each select="person">
<responsible>
<xsl:apply-templates select="../contact" />
<xsl:apply-templates select="." />
</responsible>
</xsl:for-each>
</xsl:template>
在xsl:for-each
循环下,你在上下文节点person
下,所以你必须使用点符号(..)来引用父节点。