如何为xslt的xml中的节点选择多个同名标记?
我是Xslt新手。我试图使用xslt简化xml。但我面临两个问题: 1.)相同的标记名-我无法获取第二个标记名,只获取了一个标记名(xml中的PublishItem标记) 2.)有多个嵌套的内部标记,它们具有相同的名称,我无法使用xslt获取这些名称。(xml中的itemLife标记) 这是我迄今为止创建的XSLT:如何为xslt的xml中的节点选择多个同名标记?,xml,xslt,xpath,Xml,Xslt,Xpath,我是Xslt新手。我试图使用xslt简化xml。但我面临两个问题: 1.)相同的标记名-我无法获取第二个标记名,只获取了一个标记名(xml中的PublishItem标记) 2.)有多个嵌套的内部标记,它们具有相同的名称,我无法使用xslt获取这些名称。(xml中的itemLife标记) 这是我迄今为止创建的XSLT: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="h
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Conversion</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">inventoryItemIndDD</th>
<th style="text-align:left">variableWeightInd</th>
<th style="text-align:left">itemDesc</th>
<th style="text-align:left">itemName</th>
<th style="text-align:left">environmentTypeCd</th>
<th style="text-align:left">lineNum</th>
<th style="text-align:left">itemHandlingLevelType</th>
<th style="text-align:left">Caseitem:inventoryItemIndDD</th>
<th style="text-align:left">Caseitem:variableWeightInd</th>
<th style="text-align:left">Caseitem:itemDesc</th>
<th style="text-align:left">Caseitem:itemName</th>
<th style="text-align:left">Caseitem:environmentTypeCd</th>
<th style="text-align:left">Caseitem:lineNum</th>
<th style="text-align:left">Caseitem:itemHandlingLevelType</th>
<th style="text-align:left">Caseitem:itemLife1Days</th>
<th style="text-align:left">Caseitem:itemLife2Days</th>
<th style="text-align:left">Caseitem:itemLife3Days</th>
</tr>
<xsl:for-each select="/PublishItem/body/itemObject/item">
<tr>
<td><xsl:value-of select="inventoryItemIndDD"/></td>
<td><xsl:value-of select="variableWeightInd"/></td>
<td><xsl:value-of select="itemDescription/itemDesc"/></td>
<td><xsl:value-of select="itemDescription/itemName"/></td>
<td><xsl:value-of select="itemEnvironmentRecord/environmentType/environmentTypeCd"/></td>
<td><xsl:value-of select="itemGroupMembership/itemGroup/itemGroupLine/lineNum"/></td>
<td><xsl:value-of select="itemHandlingLevelType/itemHandlingLevelTypeCd"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/inventoryItemIndDD"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/variableWeightInd"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemDesc"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemName"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/itemEnvironmentRecord/environmentType/environmentTypeCd"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/itemGroupMembership/itemGroup/itemGroupLine/lineNum"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemHandlingLevelTypeCd"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemLife/itemLifeDays"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType//itemLife/itemLifeDays"/></td>
<td><xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType//itemLife/itemLifeDays"/></td>
<!-- <xsl:for-each select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemLife"> -->
<!-- <xsl:value-of select="."/> -->
<!-- <xsl:if test="position() != last()">, </xsl:if> -->
<!-- </xsl:for-each> -->
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
您在这方面的帮助和建议将是非常好的。我认为这正是您所需要的。请试试这个
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Conversion</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">inventoryItemIndDD</th>
<th style="text-align:left">variableWeightInd</th>
<th style="text-align:left">itemDesc</th>
<th style="text-align:left">itemName</th>
<th style="text-align:left">environmentTypeCd</th>
<th style="text-align:left">lineNum</th>
<th style="text-align:left">itemHandlingLevelType</th>
<th style="text-align:left">Caseitem:inventoryItemIndDD</th>
<th style="text-align:left">Caseitem:variableWeightInd</th>
<th style="text-align:left">Caseitem:itemDesc</th>
<th style="text-align:left">Caseitem:itemName</th>
<th style="text-align:left">Caseitem:environmentTypeCd</th>
<th style="text-align:left">Caseitem:lineNum</th>
<th style="text-align:left">Caseitem:itemHandlingLevelType</th>
<th style="text-align:left">Caseitem:itemLife1Days</th>
<th style="text-align:left">Caseitem:itemLife2Days</th>
<th style="text-align:left">Caseitem:itemLife3Days</th>
</tr>
<xsl:for-each select="//PublishItem">
<xsl:for-each select="body/itemObject/item">
<tr>
<td>
<xsl:value-of select="inventoryItemIndDD"/>
</td>
<td>
<xsl:value-of select="variableWeightInd"/>
</td>
<td>
<xsl:value-of select="itemDescription/itemDesc"/>
</td>
<td>
<xsl:value-of select="itemDescription/itemName"/>
</td>
<td>
<xsl:value-of select="itemEnvironmentRecord/environmentType/environmentTypeCd"/>
</td>
<td>
<xsl:value-of select="itemGroupMembership/itemGroup/itemGroupLine/lineNum"/>
</td>
<td>
<xsl:value-of select="itemHandlingLevelType/itemHandlingLevelTypeCd"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/inventoryItemIndDD"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/variableWeightInd"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemDesc"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemName"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemEnvironmentRecord/environmentType/environmentTypeCd"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemGroupMembership/itemGroup/itemGroupLine/lineNum"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemHandlingLevelTypeCd"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemLife[1]/itemLifeDays"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemLife[2]/itemLifeDays"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemLife[3]/itemLifeDays"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
转变
目录项索引
可变权重风
itemDesc
项目名称
环境类型CD
lineNum
itemHandlingLevelType
案例项目:inventoryItemIndDD
案例项:可变权重索引
案例项目:项目描述
Caseitem:itemName
案例项目:环境类型CD
案例项目:lineNum
Caseitem:itemHandlingLevelType
案例项目:项目寿命1天
案例项目:项目寿命2天
案例项目:项目寿命3天
嘿,非常感谢您的回复和帮助!!这对你的生活非常有效。如何为多个PublishItem标记执行此操作??我也是这样做的吗?这个应该支持多个PublishItem标记。我只得到第一个标记:(您提供的示例xml没有根节点。因此我认为您需要在代码级别上循环PublishItems。哦,好的,那么我会这样做。非常感谢您的帮助。我将此标记为答案
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Conversion</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">inventoryItemIndDD</th>
<th style="text-align:left">variableWeightInd</th>
<th style="text-align:left">itemDesc</th>
<th style="text-align:left">itemName</th>
<th style="text-align:left">environmentTypeCd</th>
<th style="text-align:left">lineNum</th>
<th style="text-align:left">itemHandlingLevelType</th>
<th style="text-align:left">Caseitem:inventoryItemIndDD</th>
<th style="text-align:left">Caseitem:variableWeightInd</th>
<th style="text-align:left">Caseitem:itemDesc</th>
<th style="text-align:left">Caseitem:itemName</th>
<th style="text-align:left">Caseitem:environmentTypeCd</th>
<th style="text-align:left">Caseitem:lineNum</th>
<th style="text-align:left">Caseitem:itemHandlingLevelType</th>
<th style="text-align:left">Caseitem:itemLife1Days</th>
<th style="text-align:left">Caseitem:itemLife2Days</th>
<th style="text-align:left">Caseitem:itemLife3Days</th>
</tr>
<xsl:for-each select="//PublishItem">
<xsl:for-each select="body/itemObject/item">
<tr>
<td>
<xsl:value-of select="inventoryItemIndDD"/>
</td>
<td>
<xsl:value-of select="variableWeightInd"/>
</td>
<td>
<xsl:value-of select="itemDescription/itemDesc"/>
</td>
<td>
<xsl:value-of select="itemDescription/itemName"/>
</td>
<td>
<xsl:value-of select="itemEnvironmentRecord/environmentType/environmentTypeCd"/>
</td>
<td>
<xsl:value-of select="itemGroupMembership/itemGroup/itemGroupLine/lineNum"/>
</td>
<td>
<xsl:value-of select="itemHandlingLevelType/itemHandlingLevelTypeCd"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/inventoryItemIndDD"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/variableWeightInd"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemDesc"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemDescription/itemName"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemEnvironmentRecord/environmentType/environmentTypeCd"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemGroupMembership/itemGroup/itemGroupLine/lineNum"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemHandlingLevelType/itemHandlingLevelTypeCd"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemLife[1]/itemLifeDays"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemLife[2]/itemLifeDays"/>
</td>
<td>
<xsl:value-of select="itemHandlingStructure/parentItem/itemLife[3]/itemLifeDays"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>