Xml 提取所有节点中不同节点后的第一个和第二个节点
对于下面的xml,我需要在所有节点的一个不同名称后面的第一个和第二个循环码:Xml 提取所有节点中不同节点后的第一个和第二个节点,xml,xslt,Xml,Xslt,对于下面的xml,我需要在所有节点的一个不同名称后面的第一个和第二个循环码: <queryResult> <result> <row idx="1"> <CICode>CXXXXXXX1</CICode> <CIRevision>A</CIRevision> <CIState>WIP</CIState> <CIName>CINAME1</CINam
<queryResult>
<result>
<row idx="1">
<CICode>CXXXXXXX1</CICode>
<CIRevision>A</CIRevision>
<CIState>WIP</CIState>
<CIName>CINAME1</CIName>
<CIBrand>Clarista</CIBrand>
<CIProductRange></CIProductRange>
<CICUST>KWARC001</CICUST>
</row>
<row idx="2">
<CICode>CXXXXXXX1</CICode>
<CIRevision>B</CIRevision>
<CIState>Released</CIState>
<CIName>CINAME1</CIName>
<CIBrand>Clarista</CIBrand>
<CIProductRange></CIProductRange>
<CICUST>KWARC001</CICUST>
</row>
<row idx="3">
<CICode>CXXXXXXX2</CICode>
<CIRevision>A</CIRevision>
<CIState>Released</CIState>
<CIName>CINAME2</CIName>
<CIBrand>Clarista</CIBrand>
<CIProductRange></CIProductRange>
<CICUST>KWARC001</CICUST>
</row>
<row idx="4">
<CICode>CXXXXXXX2</CICode>
<CIRevision>B</CIRevision>
<CIState>WIP</CIState>
<CIName>CINAME2</CIName>
<CIBrand>Clarista</CIBrand>
<CIProductRange></CIProductRange>
<CICUST>KWARC001</CICUST>
</row>
<row idx="5">
<CICode>CXXXXXXX3</CICode>
<CIRevision>A</CIRevision>
<CIState>Obsolete</CIState>
<CIName>CINAME3</CIName>
<CIBrand>Clarista</CIBrand>
<CIProductRange></CIProductRange>
<CICUST>KWARC001</CICUST>
</row>
<row idx="6">
<CICode>CXXXXXXX3</CICode>
<CIRevision>B</CIRevision>
<CIState>Released</CIState>
<CIName>CINAME3</CIName>
<CIBrand>Clarista</CIBrand>
<CIProductRange></CIProductRange>
<CICUST>KWARC001</CICUST>
</row>
<row idx="7">
<CICode>CXXXXXXX3</CICode>
<CIRevision>C</CIRevision>
<CIState>WIP</CIState>
<CIName>CINAME3</CIName>
<CIBrand>Clarista</CIBrand>
<CIProductRange></CIProductRange>
<CICUST>KWARC001</CICUST>
</row>
</result>
</queryResult>
使用以下xsl,我可以提取所有不同的节点:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="no" encoding="UTF-8"/>
<!-- Generate XML output from a queryResult document -->
<xsl:template match="/">
<xsl:choose>
<xsl:when test="queryResult/result[row]">
<xsl:apply-templates />
</xsl:when>
<xsl:otherwise>Not defined</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:key name="CIProductRange" match="//*" use="local-name(.)"/>
<xsl:template match="CICode">
<xsl:if test="count(preceding::CICode)=0">
<!-- list of CI codes -->
<xsl:text>
CICODE~</xsl:text>
<xsl:for-each select="/queryResult/result/child::row">
<xsl:variable name="currentCode1" select='CICode'/>
<xsl:choose>
<!-- to get distinct CI codes -->
<xsl:when test="count(following::CICode[.=$currentCode1])=0">
<xsl:choose>
<xsl:when test="count(preceding::CICode[.!=$currentCode1])=0">
<xsl:copy-of select="$currentCode1"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="concat(',',$currentCode1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:text>~</xsl:text>
<xsl:for-each select="/queryResult/result/child::row">
<xsl:variable name="currentCode1" select='CICode'/>
<xsl:variable name="currentRevision1" select='CIRevision'/>
<xsl:choose>
<xsl:when test="count(following::CICode[.=$currentCode1])=0">
<xsl:choose>
<xsl:when test="count(preceding::CICode[.!=$currentCode1])=0">
<xsl:copy-of select='$currentRevision1'/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="concat(',',$currentRevision1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:text>~</xsl:text>
<xsl:text>
CINAME~</xsl:text>
<xsl:for-each select="/queryResult/result/child::row">
<xsl:variable name="currentName2" select='CIName'/>
<xsl:choose>
<xsl:when test="count(following::CIName[.=$currentName2])=0">
<xsl:choose>
<xsl:when test="count(preceding::CIName[.!=$currentName2])=0">
<xsl:copy-of select="$currentName2"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="concat(',',$currentName2)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:for-each>~ ~ ~no
</xsl:if>
</xsl:template>
<xsl:template match="row">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="result">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="queryResult">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*">
</xsl:template>
</xsl:stylesheet>
由于我对xslt不太熟悉,您能帮助我在所有节点的不同部分之后获取第一个和第二个循环码吗
提前感谢您的帮助 您是否仅限于XSLT1?我也不知道你在找什么,如果你能根据你展示的样品用简单的英语多解释一下你的要求,那会有帮助的。谢谢你的回复。谢谢你的回复。事实上,我需要更改此xslt以答复新的需求。事实上,xml文件包含所有循环码,我们可以有多个循环码(相同的循环码,但不同的“CIRevision”、“CIState”…)。我需要的是,对于CICode不同的列表:cxxxxxx1,cxxxxxx2,cxxxxxx3,只有第一个和第二个:cxxxxxx1,cxxxxxx2。如果不够清楚,请告诉我。非常感谢您的帮助。您还没有回答有关XSLT版本的问题,似乎在XSLT 2中以及以后的版本中,您可以使用
不同的值(//CICode)[position()le 2]
。很抱歉没有回答有关XSLT版本的问题。如何知道我是否仅限于xslt1?关于您提供的表达式,它是有效的。但是,如何获得每个不同循环码的其他信息(CIRevision、CIState…)?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="no" encoding="UTF-8"/>
<!-- Generate XML output from a queryResult document -->
<xsl:template match="/">
<xsl:choose>
<xsl:when test="queryResult/result[row]">
<xsl:apply-templates />
</xsl:when>
<xsl:otherwise>Not defined</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:key name="CIProductRange" match="//*" use="local-name(.)"/>
<xsl:template match="CICode">
<xsl:if test="count(preceding::CICode)=0">
<!-- list of CI codes -->
<xsl:text>
CICODE~</xsl:text>
<xsl:for-each select="/queryResult/result/child::row">
<xsl:variable name="currentCode1" select='CICode'/>
<xsl:choose>
<!-- to get distinct CI codes -->
<xsl:when test="count(following::CICode[.=$currentCode1])=0">
<xsl:choose>
<xsl:when test="count(preceding::CICode[.!=$currentCode1])=0">
<xsl:copy-of select="$currentCode1"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="concat(',',$currentCode1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:text>~</xsl:text>
<xsl:for-each select="/queryResult/result/child::row">
<xsl:variable name="currentCode1" select='CICode'/>
<xsl:variable name="currentRevision1" select='CIRevision'/>
<xsl:choose>
<xsl:when test="count(following::CICode[.=$currentCode1])=0">
<xsl:choose>
<xsl:when test="count(preceding::CICode[.!=$currentCode1])=0">
<xsl:copy-of select='$currentRevision1'/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="concat(',',$currentRevision1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:text>~</xsl:text>
<xsl:text>
CINAME~</xsl:text>
<xsl:for-each select="/queryResult/result/child::row">
<xsl:variable name="currentName2" select='CIName'/>
<xsl:choose>
<xsl:when test="count(following::CIName[.=$currentName2])=0">
<xsl:choose>
<xsl:when test="count(preceding::CIName[.!=$currentName2])=0">
<xsl:copy-of select="$currentName2"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="concat(',',$currentName2)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
</xsl:choose>
</xsl:for-each>~ ~ ~no
</xsl:if>
</xsl:template>
<xsl:template match="row">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="result">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="queryResult">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="*">
</xsl:template>
</xsl:stylesheet>
CICODE~CXXXXXXX1,CXXXXXXX2 ,CXXXXXXX3~B,B ,C~
CINAME~CINAME1,CINAME2 ,CINAME3~ ~ ~no