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

时间:2016-05-03 18:27:27

标签: xml xslt xslt-1.0 xslt-2.0

我有一个要求,需要复制每个子引用节点的所有父节点信息。无法弄清楚我自己。任何帮助将受到高度赞赏。这是转换代码。需要从父级复制每个“ 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>

INPUT:

 <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>

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

  <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>

答案 1 :(得分:1)

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>