2016-10-27 7 views
0

I는 다음과 같습니다 일부 XML이 :선택 노드

입니다
<root> 
    <node id="2_1" name="FOO" /> 
    <node id="2_2" class="child" /> 
    <node id="2_3" class="child" /> 
    <node id="2_4" class="child" /> 
    <node id="3_1" name="BAR" /> 
    <node id="3_2" class="child" /> 
    <node id="3_3" class="child" /> 
    <node id="3_4" class="child" /> 
</root> 

하는 child의 부모 노드 id 항상 같은 접두사를 공유하지만 _1로 끝나고을 모든 자식 노드 접미사는 2에서 시작하여 증가합니다.

I과 유사한 출력을 생성하기 위해 노력하고있어 : 내가 그룹을 얻기 위해이 XSLT를

<groups> 
    <group id="2_1"> 
    <child id="2_2" /> 
    <child id="2_3" /> 
    <child id="2_4" /> 
    </group> 
    <group id="3_1"> 
    <child id="3_2" /> 
    <child id="3_3" /> 
    <child id="3_4" /> 
    </group> 
</groups> 

을하지만 아이들을 선택하는 방법을 확실 해요 :

<xsl:template match="/"> 

<groups> 
    <xsl:for-each select="/root/node[@name]"> 
    <group> 
     <xsl:attribute name="id"> 
     <xsl:value-of select="@id" /> 
     </xsl:attribute> 
    </group> 
    </xsl:for-each> 
</groups> 

</xsl:template> 

암의 I를 이게 올바른 방향으로가는거야? 매개 변수를 허용하는 템플릿이 필요하다고 생각하지만 현재 매개 변수의 값을 id 값으로 지정하는 방법을 잘 모르겠습니다.

+0

이것은 그룹 문제입니다. XSLT 1.0 또는 XSLT 2.0을 사용하는지 여부는 일반적으로 그룹별로 다르게 처리되므로 말할 수 있습니까? 감사! –

+0

@TimC Windows 7에서 MXSML 6.0을 사용하고 있습니다. IIRC는 XSLT 1.0입니까? – ThunderFrame

+0

여기에서 시작하십시오 : http://www.jenitennison.com/xslt/grouping/muenchian.html 힌트 :'substring-before (@id, '_')'를 키 표현식으로 사용하십시오. –

답변

1

사실, 내 의견에도 불구하고 실제로 여기에서 그룹화 할 필요는 없습니다. 실제로 그룹 ID가 항상 _1으로 끝나고 더 높은 숫자로 끝나는 규칙이있는 경우 조금 단순화 할 수 있습니다.

당신은 여전히 ​​키를 생성 할

,하지만 당신은 _1 요소를 선택하여 시작하는 경우, 만

<xsl:key name="nodes" match="node" use="substring-before(@id, '_')" /> 

일치 하나, 당신은 키

<xsl:apply-templates 
    select="key('nodes', substring-before(@id, '_'))[substring-after(@id, '_') != '1']" /> 
를 사용하여 자식 요소를 얻을 수 있습니다

XSLT 사용

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:key name="nodes" match="node" use="substring-before(@id, '_')" /> 

    <xsl:template match="root"> 
     <groups> 
      <xsl:apply-templates select="node[substring-after(@id, '_') = '1']" /> 
     </groups> 
    </xsl:template> 

    <xsl:template match="node[substring-after(@id, '_') = '1']"> 
     <group id="{@id}"> 
      <xsl:apply-templates select="key('nodes', substring-before(@id, '_'))[substring-after(@id, '_') != '1']" /> 
     </group> 
    </xsl:template> 

    <xsl:template match="node"> 
     <child id="{@id}"> 
     </child> 
    </xsl:template> 
</xsl:stylesheet> 

또는 "그룹" 요소는 항상 name 특성을 가지고 있으며 하위 항목은 그렇지 않습니다.이 항목을 약간 단순화 할 수 있습니다. ...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="yes" /> 

    <xsl:key name="nodes" match="node" use="substring-before(@id, '_')" /> 

    <xsl:template match="root"> 
     <groups> 
      <xsl:apply-templates select="node[@name]" /> 
     </groups> 
    </xsl:template> 

    <xsl:template match="node[@name]"> 
     <group id="{@id}"> 
      <xsl:apply-templates select="key('nodes', substring-before(@id, '_'))[not(@name)]" /> 
     </group> 
    </xsl:template> 

    <xsl:template match="node"> 
     <child id="{@id}"> 
     </child> 
    </xsl:template> 
</xsl:stylesheet>