Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用XSLT为子节点创建新节点并复制父节点信息_Xml_Xslt_Xslt 1.0_Xslt 2.0 - Fatal编程技术网

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

Xml 使用XSLT为子节点创建新节点并复制父节点信息,xml,xslt,xslt-1.0,xslt-2.0,Xml,Xslt,Xslt 1.0,Xslt 2.0,我有一个要求,需要复制每个子引用节点的所有父节点信息。我自己搞不懂。我们将非常感谢您的帮助。这是转换代码。需要从父级复制每个“关系_410”的所有信息 输入: <Records> <Record> <Tracking_ID>7</Tracking_ID> <Relationship_397> <Field_contentId>12099237</Field_cont

我有一个要求,需要复制每个子引用节点的所有父节点信息。我自己搞不懂。我们将非常感谢您的帮助。这是转换代码。需要从父级复制每个“关系_410”的所有信息


输入:

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

7.
12099237
12102605
高
测试--12102605
测试——高
7358689
7358689
5981551
5985378
输出

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

7.
12099237
12102605
高
7358689
7358689
5981551
5985378
7.
12099237
测试--12102605
测试——高
7358689
7358689
5981551
5985378

您可以尝试以下方法:

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

您可以尝试以下方法:

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

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

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

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

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


您能将示例最小化到演示问题所需的最小值吗?看:我已经删除了所有不必要的东西。很抱歉。感谢您对它的研究。这仍然很混乱,因为您的XSLT引用了名称空间中的节点,而您的XML没有节点。我已经发布了一个完全基于显示的输入和输出的答案。您能将示例最小化到演示问题所需的最小值吗?看:我已经删除了所有不必要的东西。很抱歉。感谢您对它的研究。这仍然很混乱,因为您的XSLT引用了名称空间中的节点,而您的XML没有节点。我已经发布了一个完全基于显示的输入和输出的答案。感谢Mike提供易于理解的解决方案。感谢Mike提供易于理解的解决方案。