我在这个板上研究了各种解决方案,但恐怕我仍然有点卡住-我是否需要使用键使用Muenchian分组或将之前的兄弟姐妹帮助项目类型w
简而言之,有多个faRecord节点-每个记录中的第一个item元素表示它是什么类型的记录-这里是C, W,但也有其他类型。
到目前为止,我拥有的XSL将这些记录转换为类别和工作码,但我对如何将工作码嵌套到类别中感到困惑。顺序可以是C W W W C W C W W W等等。这些值都不是唯一的。(将会有项目嵌套在工作代码中等等——但这是第一步。每个类别将有0个或更多的工作代码。每个工作码可以有0个或多个项目。)
这是XML
<?xml version="1.0" encoding="UTF-8"?>
<EstimateDisplayRequest>
<facXML>
<faRecord>
<recordCode>33</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>C</itemValue>
</item>
<item>
<itemMapCode>80</itemMapCode>
<itemValue>0</itemValue>
</item>
<item>
<itemMapCode>81</itemMapCode>
<itemValue>INTERNAL CREATIVE</itemValue>
</item>
<item>
<itemMapCode>82</itemMapCode>
<itemValue>TOTAL INTERNAL CREAT</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>33</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>W</itemValue>
</item>
<item>
<itemMapCode>41</itemMapCode>
<itemValue>0</itemValue>
</item>
<item>
<itemMapCode>42</itemMapCode>
<itemValue>TI</itemValue>
</item>
<item>
<itemMapCode>43</itemMapCode>
<itemValue>Time Work Code</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>33</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>W</itemValue>
</item>
<item>
<itemMapCode>41</itemMapCode>
<itemValue>0</itemValue>
</item>
<item>
<itemMapCode>42</itemMapCode>
<itemValue>TI</itemValue>
</item>
<item>
<itemMapCode>43</itemMapCode>
<itemValue>Time Work Code</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>33</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>C</itemValue>
</item>
<item>
<itemMapCode>80</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>81</itemMapCode>
<itemValue>EXTERNAL CREATIVE</itemValue>
</item>
<item>
<itemMapCode>82</itemMapCode>
<itemValue>TOTAL EXTERNAL CREAT</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>33</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>W</itemValue>
</item>
<item>
<itemMapCode>41</itemMapCode>
<itemValue>0</itemValue>
</item>
<item>
<itemMapCode>42</itemMapCode>
<itemValue>TI2</itemValue>
</item>
<item>
<itemMapCode>43</itemMapCode>
<itemValue>Time Work Code</itemValue>
</item>
</faRecord>
</facXML>
</EstimateDisplayRequest>
这是我到目前为止的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="EstimateDisplayRequest">
<MCSResponse>
<EstimateDisplayResponse>
<xsl:apply-templates select="*" mode="EstimateDisplayResponse" />
</EstimateDisplayResponse>
</MCSResponse>
</xsl:template>
<xsl:template match="faRecord[item/itemMapCode='1' and item/itemValue='C']" mode="EstimateDisplayResponse">
<EstimateDisplayResponseDetailCategory>
<xsl:attribute name="id"><xsl:number/></xsl:attribute>
<xsl:apply-templates select="child::*[not(self::recordCode)]" />
</EstimateDisplayResponseDetailCategory>
</xsl:template>
<xsl:template match="faRecord[item/itemMapCode='1' and item/itemValue='W']" mode="EstimateDisplayResponse">
<EstimateDisplayResponseDetailWorkcode>
<xsl:attribute name="id"><xsl:number/></xsl:attribute>
<xsl:apply-templates select="child::*[not(self::recordCode)]" />
</EstimateDisplayResponseDetailWorkcode>
</xsl:template>
<xsl:template match="item[itemMapCode='1']" />
<xsl:template match="item[itemMapCode='80']" >
<CategoryCode>
<xsl:value-of select="./itemValue" />
</CategoryCode>
</xsl:template>
<xsl:template match="item[itemMapCode='81']" >
<CategoryName>
<xsl:value-of select="./itemValue" />
</CategoryName>
</xsl:template>
<xsl:template match="item[itemMapCode='82']" >
<CategoryTotalName>
<xsl:value-of select="./itemValue" />
</CategoryTotalName>
</xsl:template>
<xsl:template match="item[itemMapCode='41']" >
<WorkCodeCategory>
<xsl:value-of select="./itemValue" />
</WorkCodeCategory>
</xsl:template>
<xsl:template match="item[itemMapCode='42']" >
<WorkCode>
<xsl:value-of select="./itemValue" />
</WorkCode>
</xsl:template>
<xsl:template match="item[itemMapCode='43']" >
<WorkCodeName>
<xsl:value-of select="./itemValue" />
</WorkCodeName>
</xsl:template>
</xsl:stylesheet>
TIA !
[对不起,我也应该列出输出-一个类别列表。我尝试使用for-each与以下兄弟姐妹的类别,但我最终得到所有剩余的工作代码元素,因为我走下节点-这是预期的,我猜:(]
<mcsresponse>
<estimatedisplayresponse>
<estimatedisplayresponsedetailcategory id="1">
<categorycode>0</categorycode>
<categoryname>INTERNAL CREATIVE</categoryname>
<categorytotalname>TOTAL INTERNAL CREAT</categorytotalname>
<estimatedisplayresponsedetailworkcode id="2">
<workcodecategory>0</workcodecategory>
<workcode>TI</workcode>
<workcodename>Time Work Code</workcodename>
</estimatedisplayresponsedetailworkcode>
<estimatedisplayresponsedetailworkcode id="3">
<workcodecategory>0</workcodecategory>
<workcode>TI</workcode>
<workcodename>Time Work Code</workcodename>
</estimatedisplayresponsedetailworkcode>
</estimatedisplayresponsedetailcategory>
<estimatedisplayresponsedetailcategory id="4">
<categorycode>0</categorycode>
<categoryname>INTERNAL CREATIVE</categoryname>
<categorytotalname>TOTAL INTERNAL CREAT</categorytotalname>
<else>YNNYN</else>
<estimatedisplayresponsedetailworkcode id="5">
<workcodecategory>0</workcodecategory>
<workcode>TI</workcode>
<workcodename>Time Work Code</workcodename>
</estimatedisplayresponsedetailworkcode>
</estimatedisplayresponsedetailcategory>
</estimatedisplayresponse>
</mcsresponse>
XSLT 1.0转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:strip-space elements="*" />
<xsl:key name="kWork" match="faRecord[item[itemMapCode = 1]/itemValue = 'W']" use="
generate-id(preceding-sibling::faRecord[item[itemMapCode = 1]/itemValue = 'C'][1])
" />
<xsl:template match="EstimateDisplayRequest">
<MCSResponse>
<EstimateDisplayResponse>
<xsl:apply-templates select="*/faRecord[item[itemMapCode = 1]/itemValue = 'C']" />
</EstimateDisplayResponse>
</MCSResponse>
</xsl:template>
<xsl:template match="faRecord[item[itemMapCode = 1]/itemValue = 'C']">
<EstimateDisplayResponseDetailCategory>
<xsl:attribute name="id"><xsl:number/></xsl:attribute>
<xsl:apply-templates select="item" />
<xsl:apply-templates select="key('kWork', generate-id())" />
</EstimateDisplayResponseDetailCategory>
</xsl:template>
<xsl:template match="faRecord[item[itemMapCode = 1]/itemValue = 'W']">
<EstimateDisplayResponseDetailWorkcode>
<xsl:attribute name="id"><xsl:number/></xsl:attribute>
<xsl:apply-templates select="item" />
</EstimateDisplayResponseDetailWorkcode>
</xsl:template>
<!-- this catches <recordCode> and unknown elements -->
<xsl:template match="faRecord/*" />
<xsl:template match="faRecord/item">
<xsl:variable name="elemName">
<xsl:choose>
<xsl:when test="itemMapCode = 41">WorkCodeCategory</xsl:when>
<xsl:when test="itemMapCode = 42">WorkCode</xsl:when>
<xsl:when test="itemMapCode = 43">WorkCodeName</xsl:when>
<xsl:when test="itemMapCode = 80">CategoryCode</xsl:when>
<xsl:when test="itemMapCode = 81">CategoryName</xsl:when>
<xsl:when test="itemMapCode = 82">CategoryTotalName</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:if test="$elemName != ''">
<xsl:element name="{$elemName}">
<xsl:value-of select="./itemValue" />
</xsl:element>
</xsl:if>
<xsl:if test="$elemName = '' and itemMapCode != 1">
<xsl:message>Warning: no mapping for itemMapCode '<xsl:value-of select="itemMapCode" />'</xsl:message>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
给我
<MCSResponse>
<EstimateDisplayResponse>
<EstimateDisplayResponseDetailCategory id="1">
<CategoryCode>0</CategoryCode>
<CategoryName>INTERNAL CREATIVE</CategoryName>
<CategoryTotalName>TOTAL INTERNAL CREAT</CategoryTotalName>
<EstimateDisplayResponseDetailWorkcode id="2">
<WorkCodeCategory>0</WorkCodeCategory>
<WorkCode>TI</WorkCode>
<WorkCodeName>Time Work Code</WorkCodeName>
</EstimateDisplayResponseDetailWorkcode>
<EstimateDisplayResponseDetailWorkcode id="3">
<WorkCodeCategory>0</WorkCodeCategory>
<WorkCode>TI</WorkCode>
<WorkCodeName>Time Work Code</WorkCodeName>
</EstimateDisplayResponseDetailWorkcode>
</EstimateDisplayResponseDetailCategory>
<EstimateDisplayResponseDetailCategory id="4">
<CategoryCode>1</CategoryCode>
<CategoryName>EXTERNAL CREATIVE</CategoryName>
<CategoryTotalName>TOTAL EXTERNAL CREAT</CategoryTotalName>
<EstimateDisplayResponseDetailWorkcode id="5">
<WorkCodeCategory>0</WorkCodeCategory>
<WorkCode>TI2</WorkCode>
<WorkCodeName>Time Work Code</WorkCodeName>
</EstimateDisplayResponseDetailWorkcode>
</EstimateDisplayResponseDetailCategory>
</EstimateDisplayResponse>
</MCSResponse>
<xsl:key>
根据C
类型记录的唯一ID索引W
类型的每条记录。
这样就很容易通过
处理所有类型的C
记录。<xsl:apply-templates select="*/faRecord[item[itemMapCode = 1]/itemValue = 'C']" />
和它们各自的类型W
记录通过
<xsl:apply-templates select="`key('kWork', generate-id()) />