2013-03-05 9 views
1

다음과 같은 XML 문서 감안할 때 : 나는 (분쇄기, 드 구조는, 변환) 구문 분석하려면 어떻게구문 분석 XML 문서

<Element0 AttributeA="A"> 
    <Element1 AttributeB="1" AttributeC="C" AttributeD="D"> 
    <Element2>nodeValue</Element2> 
    </Element1> 
    <Element1 AttributeB="2" AttributeC="C" AttributeD="D"> 
    <Element2>nodeValue</Element2> 
    <Element3 AttributeE="E"> 
     <Element4 AttributeF="F">nodeValue</Element4> 
    </Element3> 
    </Element1> 
    . 
    . 
    . 
    . 
</Element0> 

개별 XPath의에 문서가없이 (아래 참조) XML 문서의 내용에 대한 예고?

//Element0[@AttributeA='A']/Element1[@AttributeB='1' and @AttributeC='C' and @AttributeD='D']/Element2 
//Element0[@AttributeA='A']/Element1[@AttributeB='2' and @AttributeC='C' and @AttributeD='D']/Element2 
//Element0[@AttributeA='A']/Element1[@AttributeB='2' and @AttributeC='C' and @AttributeD='D']/Element3[@AttributeE='E']/Element4[@AttributeF='F'] 

답변

1

이렇게하면됩니다. 또한 각 요소에 대해 완전히 고유 한 XPath를 얻을 수 있도록 위치를 포함하므로 형제 중 하나와 동일한 특성을 갖고 있더라도 마찬가지입니다.

XML 입력

<Element0 AttributeA="A"> 
    <Element1 AttributeB="1" AttributeC="C" AttributeD="D"> 
     <Element2>nodeValue</Element2> 
    </Element1> 
    <Element1 AttributeB="2" AttributeC="C" AttributeD="D"> 
     <Element2>nodeValue</Element2> 
     <Element3 AttributeE="E"> 
      <Element4 AttributeF="F">nodeValue</Element4> 
     </Element3> 
    </Element1> 
</Element0> 

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="text()"/> 

    <xsl:template match="*"> 
     <xsl:for-each select="ancestor-or-self::*"> 
      <xsl:value-of select="concat('/',local-name())"/> 
      <xsl:value-of select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"/> 
      <xsl:if test="@*"> 
       <xsl:text>[</xsl:text> 
       <xsl:apply-templates select="@*"/> 
       <xsl:text>]</xsl:text> 
      </xsl:if> 
     </xsl:for-each> 
     <xsl:text>&#xA;</xsl:text> 
     <xsl:apply-templates select="node()"/> 
    </xsl:template> 

    <xsl:template match="@*"> 
     <xsl:if test="position() != 1"> 
      <xsl:text> and </xsl:text> 
     </xsl:if> 
     <xsl:value-of select="concat('@',local-name(),'=&quot;',.,'&quot;')"/> 
    </xsl:template> 

</xsl:stylesheet> 

출력

/Element0[1][@AttributeA="A"] 
/Element0[1][@AttributeA="A"]/Element1[1][@AttributeB="1" and @AttributeC="C" and @AttributeD="D"] 
/Element0[1][@AttributeA="A"]/Element1[1][@AttributeB="1" and @AttributeC="C" and @AttributeD="D"]/Element2[1] 
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"] 
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element2[1] 
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element3[1][@AttributeE="E"] 
/Element0[1][@AttributeA="A"]/Element1[2][@AttributeB="2" and @AttributeC="C" and @AttributeD="D"]/Element3[1][@AttributeE="E"]/Element4[1][@AttributeF="F"] 
+0

간단하고 간단하며 신속한 ... 성공! 정확히 내가 필요한 것 (포지션 추가 포함). 감사합니다 @ Daniel-Haley! – EngineeringSQL

+0

@echoScout - 오신 것을 환영합니다! –

0

XMLStarletxml el -v file.xml로 사용하는 경우 매우 비슷한 않습니다. 값을 가진 경로와 속성을 제공합니다. 그러나 경로의 중간에 특성을 제공하지는 않으며 해당 노드가 팁일뿐입니다. 귀하의 예를 들면 다음과 같습니다.

Element0[@AttributeA='A'] 
Element0/Element1[@AttributeB='1' and @AttributeC='C' and @AttributeD='D'] 
Element0/Element1/Element2 
Element0/Element1[@AttributeB='2' and @AttributeC='C' and @AttributeD='D'] 
Element0/Element1/Element2 
Element0/Element1/Element3[@AttributeE='E'] 
Element0/Element1/Element3/Element4[@AttributeF='F'] 
+0

당신의 응답을 당신에게 알렉산더 감사드립니다. 나는 다니엘의 접근법을 따라 갔다. – EngineeringSQL