为子级创建新节点,并使用XSLT复制父级信息



我有一个要求,需要复制每个子引用节点的所有父节点信息。我自己想不通。如有任何帮助,我们将不胜感激。这是转换代码。需要从每个"Relationship_410"的父级复制所有信息。

 <xsl:template match="/ns0:Records" mode="pass2">
        <Records xmlns="http://www.tech.com/">
          <xsl:for-each select="ns0:Record">
            <xsl:for-each select="ns0:Relationship_397">
 <xsl:for-each select="ns0:Relationship_410">
              <Record>           
                <xsl:copy-of select="./node()[not(self::ns0:Relationship_410)]"></xsl:copy-of>
              </Record>
 </xsl:for-each>
            </xsl:for-each>
          </xsl:for-each>
        </Records>
      </xsl:template>

输入:

 <Records>
    <Record>
      <Tracking_ID>7</Tracking_ID>
      <Relationship_397>
        <Field_contentId>12099237</Field_contentId>       
        <Relationship_410>
          <Field_contentId>12102605</Field_contentId>          
          <Issue_Criticality>
            <Item>High</Item>
          </Issue_Criticality>         
        </Relationship_410>
            <Relationship_410>
          <Field_contentId>test -- 12102605</Field_contentId>         
          <Issue_Criticality>
            <Item>test --High</Item>
          </Issue_Criticality>          
        </Relationship_410>
        <Relationship_49>
          <Field_contentId>7358689</Field_contentId>
          <Tracking_ID>7358689</Tracking_ID>
        </Relationship_49>          
      </Relationship_397>
      <Relationship_124>
        <Field_contentId>5981551</Field_contentId>       
      </Relationship_124>
      <Relationship_124>
        <Field_contentId>5985378</Field_contentId>        
      </Relationship_124>
    </Record>
  </Records>

输出

 <Records>
  <Record>
    <Tracking_ID>7</Tracking_ID>
    <Relationship_397>
      <Field_contentId>12099237</Field_contentId>
      <Relationship_410>
        <Field_contentId>12102605</Field_contentId>
        <Issue_Criticality>
          <Item>High</Item>
        </Issue_Criticality>
      </Relationship_410>
      <Relationship_49>
        <Field_contentId>7358689</Field_contentId>
        <Tracking_ID>7358689</Tracking_ID>
      </Relationship_49>
    </Relationship_397>
    <Relationship_124>
      <Field_contentId>5981551</Field_contentId>
    </Relationship_124>
    <Relationship_124>
      <Field_contentId>5985378</Field_contentId>
    </Relationship_124>
  </Record>
  <Record>
    <Tracking_ID>7</Tracking_ID>
    <Relationship_397>
      <Field_contentId>12099237</Field_contentId>
      <Relationship_410>
        <Field_contentId>test -- 12102605</Field_contentId>
        <Issue_Criticality>
          <Item>test --High</Item>
        </Issue_Criticality>
      </Relationship_410>
      <Relationship_49>
        <Field_contentId>7358689</Field_contentId>
        <Tracking_ID>7358689</Tracking_ID>
      </Relationship_49>
    </Relationship_397>
    <Relationship_124>
      <Field_contentId>5981551</Field_contentId>
    </Relationship_124>
    <Relationship_124>
      <Field_contentId>5985378</Field_contentId>
    </Relationship_124>
  </Record>
</Records>

AFAICT,所需的输出可以很容易地通过以下方式产生:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/Records">
    <Records>
        <xsl:for-each select="//Relationship_410">
            <Record>
                <xsl:copy-of select="../../Tracking_ID"/>
                <Relationship_397>
                    <xsl:copy-of select="../../Relationship_397/Field_contentId"/>
                    <xsl:copy-of select="."/>
                    <xsl:copy-of select="../Relationship_49"/>
                </Relationship_397>
                <xsl:copy-of select="../../Relationship_124"/>
            </Record>
        </xsl:for-each>
    </Records>
</xsl:template>
</xsl:stylesheet>

您可以尝试以下操作:

  <xsl:template match="@*|node()">
    <xsl:param name="r410" />
        <xsl:copy>
          <xsl:apply-templates select="node() | @*" >
              <xsl:with-param name="r410" select="$r410" />
          </xsl:apply-templates>
        </xsl:copy>  
  </xsl:template>
  <xsl:template match="Relationship_410"> 
      <xsl:param name="r410" />
      <xsl:if test="$r410 = generate-id(.)" >
        <xsl:copy>
            <xsl:apply-templates  />
        </xsl:copy>
      </xsl:if>
  </xsl:template>
  <xsl:template match="/Records"> 
    <xsl:copy>
        <xsl:apply-templates select="//Relationship_410" mode="gen410" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="Relationship_410" mode="gen410">
      <xsl:apply-templates select="ancestor::Record">
        <xsl:with-param name="r410" select="generate-id(.)" />
      </xsl:apply-templates>
  </xsl:template>

相关内容

  • 没有找到相关文章

最新更新