2015-02-03 3 views
0
<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을 가지고있다. 내가해야할 일은 각 "손자 기록"을보고 각각에 대한 기록을 만드는 것입니다. 즉, 할아버지/아들 아래에있는 각 손자 기록에 대해 할아버지/손/손자 1이 필요하고 할아버지/손자/손자 2에게는 두 번째 사람이 필요합니다. 나는 다음과 같은 XSLT를 사용하여 두 손자 기록을 동시에 제공합니다 .XSL 변환 - 다중 자식 노드 = 다중 관계

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

알려진 내용과 예제는 무엇입니까? 예 : 3 단계 깊이 - 더있을 수 있습니까? + 부모님 아이디에 대해 이전에 물어 보았습니다 - 그들이 틀린 것이 틀림 없습니다. –

+0

3 단계는 최대한 깊게 들어갑니다. 죄송합니다. 새로운 질문을 만드는 것이 좋습니다. 나는 parentID가 정확하다고 믿는다. Record의 contentID 속성에서 나온다. – user1772421

+0

"* 나는 parentID가 맞다고 믿는다. 레코드의 contentID 속성에서 온다. *"나는 contentID = 138286에 parentId = 578859를 갖는 레코드를 기대한다. –

답변

1

왜 이렇게 복잡 할 필요가 있는지 알 수 없기 때문에 여기에 뭔가 빠져 있습니다. 나는 다음과 같은 스타일 시트 수준의 번호 입력에 대한 요청 결과를 반환합니다 믿고 :

XSLT 우리가 서로 Record 고유 @contentId 값이 있는지 가정합니다 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> 

참고.

+0

Michael - 응답 해 주셔서 감사합니다. 나는 지금 다른 문제에 직면 해있다. 할아버지 ID 또는 할아버지 기록 (나는 왜 그렇게 복잡한 지 이해하지 못한다는 것을 이해할 때마다 내 시나리오를 설명 할 수 있습니다)이 필요할 때마다 다릅니다. 이것이 콘텐츠 ID를 1 씩 늘릴 수있는 카운터가 될 수 있다는 것을 의미합니다. – user1772421