Xslt 当xsl中的每个循环都在内部时
我有一个xml,它有两个不同级别的相同元素,我需要比较两个级别中元素的值,并返回匹配元素的值。例如,我有以下xmlXslt 当xsl中的每个循环都在内部时,xslt,xslt-1.0,xslt-2.0,xslt-grouping,xslt-3.0,Xslt,Xslt 1.0,Xslt 2.0,Xslt Grouping,Xslt 3.0,我有一个xml,它有两个不同级别的相同元素,我需要比较两个级别中元素的值,并返回匹配元素的值。例如,我有以下xml <root> <profiles> <profile> <Name>xxx</Name> <Gender>Male</Gender> </profile> <profile> <Name>yyy</N
<root>
<profiles>
<profile>
<Name>xxx</Name>
<Gender>Male</Gender>
</profile>
<profile>
<Name>yyy</Name>
<Gender>Female</Gender>
</profile>
</profiles>
<subroot>
<profiles>
<profile>
<sName>xxx</sName>
<sAge>10</sAge>
</profile>
<profile>
<sName>yyy</sName>
<sAge>20</sAge>
</profile>
</profiles>
</subroot>
</root>
当循环遍历第二个元素时,我得到了第一个元素对应的性别值。第一个元素的相同值是return,对于xxx和yyy,性别返回为“男性”。有人检查此代码并告诉我此问题的任何修复方法我将使用从相应的配置文件中查找数据-例如:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="subprofile" match="profile" use="sName" />
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="profiles/profile">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="key('subprofile', Name)/sAge"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="profile" match="profile" use="Name" />
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="subroot/profiles/profile">
<xsl:copy>
<xsl:copy-of select="key('profile', sName)/*"/>
<xsl:copy-of select="sAge"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
或者,如果您更喜欢另一个方向:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="subprofile" match="profile" use="sName" />
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="profiles/profile">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="key('subprofile', Name)/sAge"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="profile" match="profile" use="Name" />
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="subroot/profiles/profile">
<xsl:copy>
<xsl:copy-of select="key('profile', sName)/*"/>
<xsl:copy-of select="sAge"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在这两种情况下,结果都是:
结果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<profile>
<Name>xxx</Name>
<Gender>Male</Gender>
<sAge>10</sAge>
</profile>
<profile>
<Name>yyy</Name>
<Gender>Female</Gender>
<sAge>20</sAge>
</profile>
</root>
xxx
男性
10
yyy
女性
20
我将使用从相应的配置文件中查找数据-例如:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="subprofile" match="profile" use="sName" />
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="profiles/profile">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="key('subprofile', Name)/sAge"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="profile" match="profile" use="Name" />
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="subroot/profiles/profile">
<xsl:copy>
<xsl:copy-of select="key('profile', sName)/*"/>
<xsl:copy-of select="sAge"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
或者,如果您更喜欢另一个方向:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="subprofile" match="profile" use="sName" />
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="profiles/profile">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:copy-of select="key('subprofile', Name)/sAge"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="profile" match="profile" use="Name" />
<xsl:template match="/root">
<xsl:copy>
<xsl:for-each select="subroot/profiles/profile">
<xsl:copy>
<xsl:copy-of select="key('profile', sName)/*"/>
<xsl:copy-of select="sAge"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
在这两种情况下,结果都是:
结果
<?xml version="1.0" encoding="UTF-8"?>
<root>
<profile>
<Name>xxx</Name>
<Gender>Male</Gender>
<sAge>10</sAge>
</profile>
<profile>
<Name>yyy</Name>
<Gender>Female</Gender>
<sAge>20</sAge>
</profile>
</root>
xxx
男性
10
yyy
女性
20
许多连接查询最好使用@michael.hor257k解释的键来处理,但更通用的方法是绑定变量:
<xsl:for-each select="//root/subroot/profiles/profile">
<xsl:variable name="outer-profile" select="."/>
<xsl-for-each select="//root/profiles/profile">
<xsl:variable name="inner-profile" select="."/>
<xsl:choose>
<xsl:when test="$outer-profile/x/y/z = $inner-profile/p/q/r">
...
</xsl:when>
</xsl:choose>
<xsl:for-each>
</xsl-for-each>
...
许多连接查询最好使用@michael.hor257k解释的键来处理,但更通用的方法是绑定变量:
<xsl:for-each select="//root/subroot/profiles/profile">
<xsl:variable name="outer-profile" select="."/>
<xsl-for-each select="//root/profiles/profile">
<xsl:variable name="inner-profile" select="."/>
<xsl:choose>
<xsl:when test="$outer-profile/x/y/z = $inner-profile/p/q/r">
...
</xsl:when>
</xsl:choose>
<xsl:for-each>
</xsl-for-each>
...
感谢您的回复。是否有其他方法用于每个循环。例如,当我尝试相同的代码xxx男10 yyy女时,如果我只需要获取输出中的元素和低于输出的元素,请编辑您的问题,并在给定示例中显示您希望获得的确切输出。感谢您的回答。是否有其他方法用于每个循环。例如,当我尝试相同的代码xxx男10 yyy女时,如果我需要仅获取输出中的元素,并且在输出下面获取输出,请编辑您的问题,并在给定示例中显示您希望获得的确切输出。