Xml 如何将值绑定到节点,然后获取该节点内的文本
这次我有一个XML和XSLT,我希望XSLT自动从同一个XML文件中的另一个节点获取值 XML: 我所期望的结果是Xml 如何将值绑定到节点,然后获取该节点内的文本,xml,xslt,Xml,Xslt,这次我有一个XML和XSLT,我希望XSLT自动从同一个XML文件中的另一个节点获取值 XML: 我所期望的结果是 |ID |Date |Country |Description1 |Description2 |Description3 | |752 |15-Oct-2013 |[BB] Antarctica [True] |P1 |P22 |P211 | |752 |15-Oct-2013
|ID |Date |Country |Description1 |Description2 |Description3 |
|752 |15-Oct-2013 |[BB] Antarctica [True] |P1 |P22 |P211 |
|752 |15-Oct-2013 |[BB] Antarctica [True] |P2 | | |
|758 |15-Oct-2013 |[BC] Abkhazia [False] |E1 |E11 |E111 |
|758 |15-Oct-2013 |[BC] Abkhazia [False] |E1 | | |
|ID |Date |Country |Description1 |Description2 |Description3 |
|752 |15-Oct-2013 |[BB] Antarctica [True] |P1 |P22 |P211 |
|752 |15-Oct-2013 |[BB] Antarctica [True] |P2 | | |
|758 |15-Oct-2013 |[BC] Abkhazia [False] |E1 |E11 |E111 |
|758 |15-Oct-2013 |[BC] Abkhazia [False] | | | |
对于国家值AA | AB应绑定到DATA\CountryList,说明{N}也应相应地绑定到说明{N}列表
**请注意,实体节点不应绑定到Description1List中的任何“RecordType=“Person”
谢谢
EDIT1 XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="country-by-code" match="CountryName" use="@code"/>
<xsl:key name="Description1-by-code" match="Description1Name" use="@Description1Id"/>
<xsl:key name="Description2-by-code" match="Description2Name" use="@Description2Id"/>
<xsl:key name="Description3-by-code" match="Description3Name" use="@Description3Id"/>
<xsl:template match="/">
<html>
<body>n
<table border="1">
<tr>
<th>ID</th>
<th>Date</th>
<th>Country</th>
<th>Description1</th>
<th>Description2</th>
<th>Description3</th>
</tr>
<xsl:for-each select="DATA/Records/Person">
<xsl:variable name="varPerson" select="."/>
<xsl:for-each select="$varPerson/Country">
<xsl:variable name="varCountry" select="."/>
<xsl:for-each select="$varPerson/Descriptions/Description|$varPerson[not ($varPerson/Descriptions/Description)]">
<xsl:variable name="varDescription" select="."/>
<tr>
<td>
<xsl:value-of select="$varPerson/@id"/>
</td>
<td>
<xsl:value-of select="$varPerson/@date"/>
</td>
<td>
[<xsl:value-of select="$varCountry/@CountryType"/>]
<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@name"/>
[<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@IsT"/>]
</td>
<td>
<xsl:value-of select="key('Description1-by-code', $varDescription/@Description1)"/>
</td>
<td>
<xsl:value-of select="key('Description2-by-code', $varDescription/@Description2)"/>
</td>
<td>
<xsl:value-of select="key('Description3-by-code', $varDescription/@Description3)"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
<!-- -->
<xsl:for-each select="DATA/Records/Entity">
<xsl:variable name="varEntity" select="."/>
<xsl:for-each select="$varEntity/Country">
<xsl:variable name="varCountry" select="."/>
<xsl:for-each select="$varEntity/Descriptions/Description|$varEntity[not ($varEntity/Descriptions/Description)]">
<xsl:variable name="varDescription" select="."/>
<tr>
<td>
<xsl:value-of select="$varEntity/@id"/>
</td>
<td>
<xsl:value-of select="$varEntity/@date"/>
</td>
<td>
[<xsl:value-of select="$varCountry/@CountryType"/>]
<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@name"/>
[<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@IsT"/>]
</td>
<td>
<xsl:value-of select="key('Description1-by-code', $varDescription/@Description1)"/>
</td>
<td>
<xsl:value-of select="key('Description2-by-code', $varDescription/@Description2)"/>
</td>
<td>
<xsl:value-of select="key('Description3-by-code', $varDescription/@Description3)"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
实际结果是
|ID |Date |Country |Description1 |Description2 |Description3 |
|752 |15-Oct-2013 |[BB] Antarctica [True] |P1 |P22 |P211 |
|752 |15-Oct-2013 |[BB] Antarctica [True] |P2 | | |
|758 |15-Oct-2013 |[BC] Abkhazia [False] |P1 |E11 |E111 |
|758 |15-Oct-2013 |[BC] Abkhazia [False] | | | |
将此定义添加到样式表顶部(任何模板之外):
然后,而不是:
<td>
[<xsl:value-of select="normalize-space($varCountry/@CountryType)"/>]
<xsl:value-of select="normalize-space($varCountry/CountryValue)"/>
</td>
[]
使用:
[]
[]
另外,我不明白你为什么需要所有这些normalize-space()
编辑: 关于你的其他问题,我建议你简化一下你的结构——很多事情会变得更容易:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="country-by-code" match="CountryName" use="@code"/>
<xsl:key name="Description1-by-id" match="Description1Name" use="@Description1Id"/>
<xsl:key name="Description2-by-id" match="Description2Name" use="@Description2Id"/>
<xsl:key name="Description3-by-id" match="Description3Name" use="@Description3Id"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>Date</th>
<th>Country</th>
<th>Description1</th>
<th>Description2</th>
<th>Description3</th>
</tr>
<xsl:apply-templates select="DATA/Records/*"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Person | Entity">
<xsl:variable name="varRrecord" select="."/>
<xsl:for-each select="$varRrecord/Country">
<xsl:variable name="varCountry" select="."/>
<xsl:for-each select="$varRrecord/Descriptions/Description | $varRrecord[not(Descriptions/Description)]">
<xsl:variable name="varDescription" select="."/>
<tr>
<td><xsl:value-of select="$varRrecord/@id"/></td>
<td><xsl:value-of select="$varRrecord/@date"/></td>
<td>
[<xsl:value-of select="$varCountry/@CountryType"/>]
<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@name"/>
[<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@IsT"/>]
</td>
<td><xsl:value-of select="key('Description1-by-id', $varDescription/@Description1)"/></td>
<td><xsl:value-of select="key('Description2-by-id', $varDescription/@Description2)"/></td>
<td><xsl:value-of select="key('Description3-by-id', $varDescription/@Description3)"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
身份证件
日期
国家
说明1
说明2
说明3
[]
[]
编辑2: 关于描述1,我建议您更改以下内容:
<xsl:key name="Description1-by-id" match="Description1Name" use="@Description1Id"/>
致:
这是:
<td><xsl:value-of select="key('Description1-by-id', $varDescription/@Description1)"/></td>
致:
我不知道其他两个描述需要做什么(如果有的话)。实现上述两个更改后获得的结果与您(最新)的预期结果相匹配。谢谢,根据您的建议,我更改了xslt文件(在主post Edit1中更新),但此文件仍然没有避免使用“实体节点”来获取仅用于“Person”的值,例如将XML第9行更新为E1,将第40行更新为。我能把一个逻辑放在什么地方吗?谢谢lot@user3724711我不确定我是否完全理解这个问题。我得到的结果与你预期的结果相同;如果有可能出错,请更新您的输入和预期输出,以便错误变得明显。@user3724711事实上,我很确定Description2List和Description3List都没有意义:为什么Description2Name下有Description1Id-但Description1List中没有记录类型?您好,刚刚更新了xml,在文章的最后,第3行中有一条不同的记录。我不知道名单是什么样的:(其他人只传给我.)谢谢
<xsl:key name="country-by-code" match="CountryName" use="@code" />
<td>
[<xsl:value-of select="normalize-space($varCountry/@CountryType)"/>]
<xsl:value-of select="normalize-space($varCountry/CountryValue)"/>
</td>
<td>
[<xsl:value-of select="$varCountry/@CountryType"/>]
<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@name"/>
[<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@IsT"/>]
</td>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="country-by-code" match="CountryName" use="@code"/>
<xsl:key name="Description1-by-id" match="Description1Name" use="@Description1Id"/>
<xsl:key name="Description2-by-id" match="Description2Name" use="@Description2Id"/>
<xsl:key name="Description3-by-id" match="Description3Name" use="@Description3Id"/>
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>Date</th>
<th>Country</th>
<th>Description1</th>
<th>Description2</th>
<th>Description3</th>
</tr>
<xsl:apply-templates select="DATA/Records/*"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Person | Entity">
<xsl:variable name="varRrecord" select="."/>
<xsl:for-each select="$varRrecord/Country">
<xsl:variable name="varCountry" select="."/>
<xsl:for-each select="$varRrecord/Descriptions/Description | $varRrecord[not(Descriptions/Description)]">
<xsl:variable name="varDescription" select="."/>
<tr>
<td><xsl:value-of select="$varRrecord/@id"/></td>
<td><xsl:value-of select="$varRrecord/@date"/></td>
<td>
[<xsl:value-of select="$varCountry/@CountryType"/>]
<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@name"/>
[<xsl:value-of select="key('country-by-code', $varCountry/CountryValue)/@IsT"/>]
</td>
<td><xsl:value-of select="key('Description1-by-id', $varDescription/@Description1)"/></td>
<td><xsl:value-of select="key('Description2-by-id', $varDescription/@Description2)"/></td>
<td><xsl:value-of select="key('Description3-by-id', $varDescription/@Description3)"/></td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<xsl:key name="Description1-by-id" match="Description1Name" use="@Description1Id"/>
<xsl:key name="Description1" match="Description1Name" use="concat(@Description1Id, '|', @RecordType)"/>
<td><xsl:value-of select="key('Description1-by-id', $varDescription/@Description1)"/></td>
<td><xsl:value-of select="key('Description1', concat($varDescription/@Description1, '|', name($varRrecord)))"/></td>