XSL转换 - 多个子节点=多个关系

时间:2015-02-03 00:22:32

标签: xml xslt

<Records count="1">
  <Metadata>
    <FieldDefinitions>
      <FieldDefinition id="25675" name="GrandpaID" alias="GrandpaID" />
      <FieldDefinition id="123" name="Father ID" alias="FatherID" />
      <FieldDefinition id="1923" name="Son ID" alias="SonID" />
    </FieldDefinitions>
  </Metadata>
  <LevelCounts>
    <LevelCount id="1" count="2" />
    <LevelCount id="2" count="2" />
    <LevelCount id="3" count="3" />
  </LevelCounts>
  <Record contentId="578859" levelId="1" moduleId="648" parentId="0">
    <Record contentId="138286" levelId="2" moduleId="68" parentId="0">
      <Record contentId="107826" levelId="3" moduleId="152" parentId="0">
        <Field id="1923" type="1">Grandson Record 1</Field>
      </Record>
      <Record contentId="107830" levelId="3" moduleId="152" parentId="0">
        <Field id="1923" type="1">Grandson Record 2</Field>
      </Record>
      <Field id="123" type="1">Son Record</Field>
    </Record>
    <Field id="25675" type="6">Grandpa Record</Field>
  </Record>
</Records>

我有上述XML。我需要做的是看看每个&#34;孙子记录&#34;并为每个创建一个记录。也就是说,对于爷爷/儿子下的每个孙子记录,我需要爷爷/儿子/孙子1和爷爷/儿子/孙子2的第二个。我有以下XSLT同时给我两个孙子记录

XSLT:

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name ="fields" select="//Metadata/FieldDefinitions" />

  <!--match the root node-->
  <xsl:template match="Records">
    <ArcherRecords >
      <xsl:apply-templates select="Record" />
    </ArcherRecords>
  </xsl:template>

    <!-- match child relationships -->
  <xsl:template match="Record">
    <xsl:variable name="fieldName" select="translate(@levelId, ': ', '__')" />
    <xsl:element name="Relationship_{$fieldName}">
      <xsl:apply-templates select="@contentId" />
      <xsl:apply-templates select="Field" />
      <xsl:apply-templates select="Record" />
    </xsl:element>
  </xsl:template>

  <!--get field name-->
  <xsl:template name="getName">
    <xsl:param name="fieldId" />

    <xsl:choose>
      <xsl:when test="$fields/FieldDefinition[@id=$fieldId]">        
        <xsl:value-of select="$fields/FieldDefinition[@id=$fieldId]/@alias"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'Field_'"/>
        <xsl:value-of select="translate(@id, ': ', '__')" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<ArcherRecords xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Relationship_1>
      <Field_contentId>578859</Field_contentId>
      <GrandpaID>Grandpa Record</GrandpaID>
      <Relationship_2>
         <Field_contentId>138286</Field_contentId>
         <FatherID>Son Record</FatherID>
         <Relationship_3>
            <Field_contentId>107826</Field_contentId>
            <SonID>Grandson Record 1</SonID>
         </Relationship_3>
         <Relationship_3>
            <Field_contentId>107830</Field_contentId>
            <SonID>Grandson Record 2</SonID>
         </Relationship_3>
      </Relationship_2>
   </Relationship_1>
</ArcherRecords>

期望的结果:

<?xml version="1.0" encoding="UTF-8"?>
<ArcherRecords xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Relationship_1>
      <Field_contentId>578859</Field_contentId>
      <GrandpaID>Grandpa Record</GrandpaID>
      <Relationship_2>
         <Field_contentId>138286</Field_contentId>
         <FatherID>Son Record</FatherID>
         <Relationship_3>
            <Field_contentId>107826</Field_contentId>
            <SonID>Grandson Record 1</SonID>
         </Relationship_3>
      </Relationship_2>
   </Relationship_1>
   <Relationship_1>
      <Field_contentId>578859</Field_contentId>
      <GrandpaID>Grandpa Record</GrandpaID>
      <Relationship_2>
         <Field_contentId>138286</Field_contentId>
         <FatherID>Son Record</FatherID>    
            <Relationship_3>
                <Field_contentId>107830</Field_contentId>
                <SonID>Grandson Record 2</SonID>
         </Relationship_3>
      </Relationship_2>
   </Relationship_1>
</ArcherRecords>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我可能在这里遗漏了一些东西,因为我无法理解为什么这需要如此复杂。我相信以下样式表将返回所请求的结果,对于具有任意数量级别的输入:

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

<xsl:template match="/Records">
    <ArcherRecords>
        <!-- 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>
    </ArcherRecords>
</xsl:template>     

<xsl:template match="Record">
    <xsl:param name="leafId"/>
    <xsl:element name="Relationship_{@levelId}">
        <!-- deal with the current level -->
        <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>

请注意,我们假设每个Record都有唯一的@contentId值。