创建系统ID - XSLT

时间:2015-03-07 20:51:31

标签: xml xslt

我有一个XML,在转换后给了我想要的结果。但是,我需要生成主键或跟踪ID的想法。

XML:

<Records count="5">
  <Metadata>
    <FieldDefinitions>
      <FieldDefinition id="25675" guid="292e6d6f-0ae2-4cd6-b14b-7a5210ebbffd" name="Risk and Control Assessment ID" alias="Tracking_ID" />
      <FieldDefinition id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" name="Risk" alias="Risk" />
    </FieldDefinitions>
  </Metadata>
  <LevelCounts>
    <LevelCount id="444" guid="ed7718c6-57ea-49a4-839e-a0a4da48e8ce" count="5" />
    <LevelCount id="98" guid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" count="14" />
  </LevelCounts>
  <Record contentId="578859" levelId="444" levelGuid="ed7718c6-57ea-49a4-839e-a0a4da48e8ce" moduleId="648" parentId="0">
    <Record contentId="107826" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Access Control</Field>
    </Record>
    <Record contentId="107830" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Automation Strategy</Field>
    </Record>
    <Field id="25675" guid="292e6d6f-0ae2-4cd6-b14b-7a5210ebbffd" type="6">578859</Field>
  </Record>
  <Record contentId="578887" levelId="444" levelGuid="ed7718c6-57ea-49a4-839e-a0a4da48e8ce" moduleId="648" parentId="0">
    <Record contentId="107840" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Financial Oversight</Field>
    </Record>
    <Field id="25675" guid="292e6d6f-0ae2-4cd6-b14b-7a5210ebbffd" type="6">578887</Field>
  </Record>
  <Record contentId="578891" levelId="444" levelGuid="ed7718c6-57ea-49a4-839e-a0a4da48e8ce" moduleId="648" parentId="0">
    <Record contentId="578876" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">**Instance Test Case</Field>
    </Record>
    <Record contentId="534783" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">2013 HIPAA Revisions</Field>
    </Record>
    <Record contentId="107836" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Executive Sponsorship</Field>
    </Record>
    <Record contentId="107866" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Human Resources Management</Field>
    </Record>
    <Field id="25675" guid="292e6d6f-0ae2-4cd6-b14b-7a5210ebbffd" type="6">578891</Field>
  </Record>
  <Record contentId="578892" levelId="444" levelGuid="ed7718c6-57ea-49a4-839e-a0a4da48e8ce" moduleId="648" parentId="0">
    <Record contentId="578876" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">**Instance Test Case</Field>
    </Record>
    <Record contentId="107874" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Customer Delivery</Field>
    </Record>
    <Record contentId="107843" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Incident Management</Field>
    </Record>
    <Record contentId="549977" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Losses associated with customer relationships not legally perfected with adequate care</Field>
    </Record>
    <Field id="25675" guid="292e6d6f-0ae2-4cd6-b14b-7a5210ebbffd" type="6">578892</Field>
  </Record>
  <Record contentId="578894" levelId="444" levelGuid="ed7718c6-57ea-49a4-839e-a0a4da48e8ce" moduleId="648" parentId="0">
    <Record contentId="107866" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Human Resources Management</Field>
    </Record>
    <Record contentId="107853" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Performance Monitoring</Field>
    </Record>
    <Record contentId="107863" levelId="98" levelGuid="6a2fc306-1d9e-4921-b5a8-a608fdd92121" moduleId="152" parentId="0">
      <Field id="1923" guid="753702be-5b1e-4cf4-a2e8-3e7e7950c19b" type="1">Separation of Duties - Responsibilities</Field>
    </Record>
    <Field id="25675" guid="292e6d6f-0ae2-4cd6-b14b-7a5210ebbffd" type="6">578894</Field>
  </Record>
</Records>

我的XSLT

    <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:key name="fieldDefinition" match="FieldDefinition" use="@id" />

<xsl:template match="/Records">
    <Records>
        <!-- for each leaf node -->
        <xsl:for-each select=".//Record[not(Record)]">
            <!-- replicate the tree, starting from the top -->
            <xsl:apply-templates select="ancestor::Record[last()]">
                <xsl:with-param name="leafId" select="@contentId"/>
            </xsl:apply-templates>
        </xsl:for-each>
    </Records>
</xsl:template>     

<xsl:template match="Record">
    <xsl:param name="leafId"/>
    <xsl:element name="Relationship_{@levelId}">
        <!-- deal with the current level -->
        <New_Tracking_ID><xsl:value-of select="position()"/></New_Tracking_ID>
        <Field_contentId>
            <xsl:value-of select="@contentId" />
        </Field_contentId>

        <xsl:element name="{key('fieldDefinition', Field/@id)/@alias}">
            <xsl:value-of select="Field" />
        </xsl:element>
        <!-- continue to the next lower level, branching to current leaf node only -->
        <xsl:apply-templates select="Record[descendant-or-self::Record/@contentId=$leafId]">
            <xsl:with-param name="leafId" select="$leafId"/>
        </xsl:apply-templates>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

最后,我的结果:

<?xml version="1.0" encoding="UTF-8"?>
<Records>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578859</Field_contentId>
      <Tracking_ID>578859</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107826</Field_contentId>
         <Risk>Access Control</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578859</Field_contentId>
      <Tracking_ID>578859</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107830</Field_contentId>
         <Risk>Automation Strategy</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578887</Field_contentId>
      <Tracking_ID>578887</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107840</Field_contentId>
         <Risk>Financial Oversight</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578891</Field_contentId>
      <Tracking_ID>578891</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>578876</Field_contentId>
         <Risk>**Instance Test Case</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578891</Field_contentId>
      <Tracking_ID>578891</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>534783</Field_contentId>
         <Risk>2013 HIPAA Revisions</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578891</Field_contentId>
      <Tracking_ID>578891</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107836</Field_contentId>
         <Risk>Executive Sponsorship</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578891</Field_contentId>
      <Tracking_ID>578891</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107866</Field_contentId>
         <Risk>Human Resources Management</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578892</Field_contentId>
      <Tracking_ID>578892</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>578876</Field_contentId>
         <Risk>**Instance Test Case</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578892</Field_contentId>
      <Tracking_ID>578892</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107874</Field_contentId>
         <Risk>Customer Delivery</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578892</Field_contentId>
      <Tracking_ID>578892</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107843</Field_contentId>
         <Risk>Incident Management</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578892</Field_contentId>
      <Tracking_ID>578892</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>549977</Field_contentId>
         <Risk>Losses associated with customer relationships not legally perfected with adequate care</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578894</Field_contentId>
      <Tracking_ID>578894</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107866</Field_contentId>
         <Risk>Human Resources Management</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578894</Field_contentId>
      <Tracking_ID>578894</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107853</Field_contentId>
         <Risk>Performance Monitoring</Risk>
      </Relationship_98>
   </Relationship_444>
   <Relationship_444>
      <New_Tracking_ID>1</New_Tracking_ID>
      <Field_contentId>578894</Field_contentId>
      <Tracking_ID>578894</Tracking_ID>
      <Relationship_98>
         <New_Tracking_ID>1</New_Tracking_ID>
         <Field_contentId>107863</Field_contentId>
         <Risk>Separation of Duties - Responsibilities</Risk>
      </Relationship_98>
   </Relationship_444>
</Records>

我需要的是让New_Tracking_ID成为一系列生成的数字,这些数字本质上是连续的。最好只存在于Relationship_444中,但我相信这并不重要。我尝试了一些有效的方法,但是在关系_444节点之外使用position()放置了创建的ID。任何帮助都将非常感谢!!

1 个答案:

答案 0 :(得分:0)

当您在匹配<New_Tracking_ID><xsl:value-of select="position()"/></New_Tracking_ID>的模板中执行Record时,您总是得到1,因为 position()会返回当前节点集中当前节点的位置 ,不一定是当前节点在其兄弟姐妹中的位置。

在样式表中调用

<xsl:apply-templates select="ancestor::Record[last()]">
    <xsl:with-param name="leafId" select="@contentId"/>
</xsl:apply-templates>
当前节点集是包含ancestor::Record[last()]选择的所有节点的节点,即仅包含位置为1的单个节点的集合。

所以:

  • 为了对“leaf”Report节点进行顺序枚举,您可以使用他们的位置(因为您已经遍历它们),例如将其作为附加参数
  • 要避免New_Tracking_ID内的其他Relationship_98元素,请勿在{{1​​}}模板中调用xsl:apply-templates时传递新参数,并检查其值

XSLT 1.0

Record