XSL使用当前的上下文ID pt 2在xml树中查找其他节点值

时间:2012-08-18 00:52:41

标签: xml xslt

这是一个稍微更新过的问题,最初发布在此处:XSL - Finding another nodes value using the value of the current context。 我有一个XML,它使用PackageID值来引用文档中的其他节点。

我想为每个具有“Rasta”操作值的CompositionPackage创建for-each循环。注意:CompositionPackage可以有多个匹配。 我想使用当前节点的结果,然后在文档中找到包含返回的PackageID值的另一个节点的值。 在这种情况下,我想获取SourcePackage / PackageName或MaterialPackage / PackageName或另一个CompositionPackage的值,该值具有与CompositionPackage / PackageTracks / TimelineTrack / TrackSegment / SourcePackageID中找到的PackageID值匹配的。

示例XML:

<AAG version="1.1">
<Preface>
  <ContentStorage>
      <SourcePackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <ComponentLength>5066</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>Name001</PackageName>
        <PackageID>urn:44f70006</PackageID>
      </SourcePackage>
      <MaterialPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
            <ComponentLength>804</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>Name010</PackageName>
        <PackageID>urn:44f70008</PackageID>
      </MaterialPackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <Operation>Rasta</Operation>
                <SourcePackageID>urn:44f70006</SourcePackageID>
                <ComponentLength>10099</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
          <TimelineTrack>
            <TrackSegment>
                <Operation>NotRasta</Operation>
                <SourcePackageID>urn:44f70007</SourcePackageID>
                <ComponentLength>100</ComponentLength>
            </TrackSegment>
          <TimelineTrack>
            <TrackSegment>
                <Operation>Rasta</Operation>
                <SourcePackageID>urn:44f70008</SourcePackageID>
                <ComponentLength>200</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameABC</PackageName>
        <PackageID>urn:ce1904fd</PackageID>
      </CompositionPackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <Operation>Rasta</Operation>
                <SourcePackageID>urn:00f70009</SourcePackageID>
                <ComponentLength>222</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameABC</PackageName>
        <PackageID>urn:ff1603fd</PackageID>
      </CompositionPackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <Operation>Rasta</Operation>
                <SourcePackageID>urn:11f80008</SourcePackageID>
                <ComponentLength>444</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameBCD</PackageName>
        <PackageID>urn:gg1702fj</PackageID>
      </CompositionPackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <Operation>Rasta</Operation>
                <SourcePackageID>urn:22f90009</SourcePackageID>
                <ComponentLength>555</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameXYZ</PackageName>
        <PackageID>urn:11f80010</PackageID>
      </CompositionPackage>
      <CompositionPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
                <Operation>NotRasta</Operation>
                <SourcePackageID>urn:22f90009</SourcePackageID>
                <ComponentLength>555</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>NameWWW</PackageName>
        <PackageID>urn:11f80008</PackageID>
      </CompositionPackage>
      <MaterialPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
            <ComponentLength>864</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>Name008</PackageName>
        <PackageID>urn:00f70009</PackageID>
      </MaterialPackage>
      <MaterialPackage>
        <PackageTracks>
          <TimelineTrack>
            <TrackSegment>
            <ComponentLength>864</ComponentLength>
            </TrackSegment>
          </TimelineTrack>
        </PackageTracks>
        <PackageName>Name002</PackageName>
        <PackageID>urn:22f70009</PackageID>
      </MaterialPackage>
  </ContentStorage>
</Preface>
</AAG>

示例XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

 <xsl:template match = "AAG">
 <Master>
 <xsl:for-each select="AAG/Preface/ContentStorage/CompositionPackage/PackageTracks/TimelineTrack/TrackSegment/Operation"> <!--whose Operation value is 'Rasta' -->
 <newnode>
   <value1><xsl:value-of select ="CompositionPackage/PackageTracks/TimelineTrack/TrackSegment/ComponentLength"/></value1>
   <value2><xsl:value-of select ="CompositionPackage/PackageTracks/TimelineTrack/TrackSegment/SourcePackageID"/></value2>
   <value3><xsl:value-of select ="CompositionPackage/PackageTracks/TimelineTrack/TrackSegment[SourcePackageID=current()/MaterialPackage/PackageName"/></value3>
 </newnode>
 </xsl:for-each>
 </Master>
 </xsl:template>
 </xsl:stylesheet>

预期的XML:

<Master>
<newnode>
  <Value1>1099</Value1>
  <Value2>urn:44f70006</Value2>
  <Value3>Name001</Value3>
</newnode>
<newnode>
  <Value1>200</Value1>
  <Value2>urn:44f70008</Value2>
  <Value3>Name010</Value3>
</newnode>
<newnode>
  <Value1>222</Value1>
  <Value2>urn:00f70009</Value2>
  <Value3>Name008</Value3>
</newnode>
<newnode>
  <Value1>444</Value1>
  <Value2>urn:11f80008</Value2>
  <Value3>NameWWW</Value3>
</newnode>
<newnode>
  <Value1>555</Value1>
  <Value2>urn:22f90009</Value2>
  <Value3>Name002</Value3>
</newnode>
</Master>

My Current XSL为所有value3元素返回“”。

1 个答案:

答案 0 :(得分:3)

错误很明显:在

<xsl:for-each select=
"AAG
  /Preface
    /ContentStorage
      /CompositionPackage
         /PackageTracks/TimelineTrack/TrackSegment/Operation">

你有

<xsl:value-of select =
 "CompositionPackage/PackageTracks/TimelineTrack/TrackSegment/ComponentLength"/>

由于Operation元素没有CompositionPackage个子元素(实际上根本没有任何元素子元素),所以上面没有选择任何内容输出

xsl:value-of

正文中其他两条xsl:for-each指令的情况完全相同

提供的XML文档与想要的结果之间存在矛盾,并且存在数据错误

  1. 任何PackageID元素都没有值&#34; urn:22f90009&#34;。因此,在提供的结果的最后newnode中,Value3必须为空 - 但在提供的结果中,它具有非空字符串值。

  2. 在源XML文档中有嵌套的TimelineTrack元素。

  3. 这些问题解释了为什么我提出的转换并没有完全产生想要的结果

    <xsl:stylesheet version="1.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
          <xsl:strip-space elements="*"/>
    
          <xsl:template match="@*|node()">
                <xsl:copy>
                  <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
          </xsl:template>
    
          <xsl:template match = "AAG">
           <Master>
             <xsl:for-each select=
             "Preface
               /ContentStorage
                /CompositionPackage
                   /PackageTracks
                     /TimelineTrack
                       /TrackSegment[Operation='Rasta']
             ">
                 <newnode>
                   <value1>
                      <xsl:value-of select="ComponentLength"/>
                   </value1>
                   <value2>
                     <xsl:value-of select="SourcePackageID"/>
               </value2>
                   <value3>
                     <xsl:value-of select=
                      "../../../../*[self::SourcePackage
                           or self::MaterialPackage
                           or self::CompositionPackage]
                                        [PackageID
                                        =
                                        current()/SourcePackageID
                                        ]
                                        /PackageName
                         "/>
                   </value3>
                 </newnode>
           </xsl:for-each>
          </Master>
          </xsl:template>
    </xsl:stylesheet>
    

    在提供的XML文档上应用此转换时

    <AAG version="1.1">
        <Preface>
            <ContentStorage>
                <SourcePackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <ComponentLength>5066</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>Name001</PackageName>
                    <PackageID>urn:44f70006</PackageID>
                </SourcePackage>
                <MaterialPackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <ComponentLength>804</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>Name010</PackageName>
                    <PackageID>urn:44f70008</PackageID>
                </MaterialPackage>
                <CompositionPackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <Operation>Rasta</Operation>
                                <SourcePackageID>urn:44f70006</SourcePackageID>
                                <ComponentLength>10099</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                        <TimelineTrack>
                            <TrackSegment>
                                <Operation>NotRasta</Operation>
                                <SourcePackageID>urn:44f70007</SourcePackageID>
                                <ComponentLength>100</ComponentLength>
                            </TrackSegment>
                            <TimelineTrack>
                                <TrackSegment>
                                    <Operation>Rasta</Operation>
                                    <SourcePackageID>urn:44f70008</SourcePackageID>
                                    <ComponentLength>200</ComponentLength>
                                </TrackSegment>
                            </TimelineTrack>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>NameABC</PackageName>
                    <PackageID>urn:ce1904fd</PackageID>
                </CompositionPackage>
                <CompositionPackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <Operation>Rasta</Operation>
                                <SourcePackageID>urn:00f70009</SourcePackageID>
                                <ComponentLength>222</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>NameABC</PackageName>
                    <PackageID>urn:ff1603fd</PackageID>
                </CompositionPackage>
                <CompositionPackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <Operation>Rasta</Operation>
                                <SourcePackageID>urn:11f80008</SourcePackageID>
                                <ComponentLength>444</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>NameBCD</PackageName>
                    <PackageID>urn:gg1702fj</PackageID>
                </CompositionPackage>
                <CompositionPackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <Operation>Rasta</Operation>
                                <SourcePackageID>urn:22f90009</SourcePackageID>
                                <ComponentLength>555</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>NameXYZ</PackageName>
                    <PackageID>urn:11f80010</PackageID>
                </CompositionPackage>
                <CompositionPackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <Operation>NotRasta</Operation>
                                <SourcePackageID>urn:22f90009</SourcePackageID>
                                <ComponentLength>555</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>NameWWW</PackageName>
                    <PackageID>urn:11f80008</PackageID>
                </CompositionPackage>
                <MaterialPackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <ComponentLength>864</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>Name008</PackageName>
                    <PackageID>urn:00f70009</PackageID>
                </MaterialPackage>
                <MaterialPackage>
                    <PackageTracks>
                        <TimelineTrack>
                            <TrackSegment>
                                <ComponentLength>864</ComponentLength>
                            </TrackSegment>
                        </TimelineTrack>
                    </PackageTracks>
                    <PackageName>Name002</PackageName>
                    <PackageID>urn:22f70009</PackageID>
                </MaterialPackage>
            </ContentStorage>
        </Preface>
    </AAG>
    

    产生以下结果:

    <Master>
        <newnode>
            <value1>10099</value1>
            <value2>urn:44f70006</value2>
            <value3>Name001</value3>
        </newnode>
        <newnode>
            <value1>222</value1>
            <value2>urn:00f70009</value2>
            <value3>Name008</value3>
        </newnode>
        <newnode>
            <value1>444</value1>
            <value2>urn:11f80008</value2>
            <value3>NameWWW</value3>
        </newnode>
        <newnode>
            <value1>555</value1>
            <value2>urn:22f90009</value2>
            <value3></value3>
        </newnode>
    </Master>