2012-01-29 3 views
1
내가 XML 파일에서 특정 노드를 찾을 필요가

, <example>Some text</example>XSLT 또는 XPath : 특정 태그와 텍스트가있는 노드를 찾아서 새 XML 파일로 추출하는 방법은 무엇입니까?

은 그 때 나는이 노드를 추출 할과는 XML 파일에서 하위 요소를 그리고 새 XML 파일로 작성하고 난 후 남아있는 다른 추출해야 원본 xml 파일의 xml 노드에서 추출 노드를 뺀 다음 추출 된 노드 다음의 새 xml 파일로 추출합니다.

어떻게 이것을 xslt 또는 xpath로 할 수 있습니까? 여기

답변

2

는 어떻게 출력 모든 노드를 뺀 특정 노드와 노드의 하위 트리을에 :

이 변환 (없음이 제공되지 않았습니다!) 다음과 같은 XML 문서에 적용
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="node()|@*"> 
     <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="example[. = 'Some text']"/> 
</xsl:stylesheet> 

:

<a> 
      <example>Some different text</example> 
    <b> 
     <c> 
      <example>Some text</example> 
     </c> 
      <example>Some other text</example> 
    </b> 
</a> 

원하는 올바른 결과가 생성됩니다. :

01 여기 23,516,
<a> 
    <b> 
     <c></c> 
    </b> 
</a> 

만 원하는 노드와 노드의 서브 트리을 추출하는 방법입니다 :

<example>Some text</example> 
: 같은 XML 문서 (위), 결과는 지금
입니다 적용

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

<xsl:template match="example[. = 'Some text']"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

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

하나의 XSLT 1.0 변환에서 두 가지 다른 결과 문서를 생성 할 수 없습니다.

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:variable name="vResult2"> 
    <xsl:apply-templates mode="result2"/> 
</xsl:variable> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="example[. = 'Some text']"/> 

<xsl:template match="/"> 
    <xsl:apply-templates/> 

    <xsl:result-document href="file:///c:/temp/delete/result2.xml"> 
    <xsl:sequence select="$vResult2"/> 
    </xsl:result-document> 
</xsl:template> 

<xsl:template mode="result2" match="example[. = 'Some text']"> 
    <xsl:copy-of select="."/> 
</xsl:template> 

<xsl:template mode="result2" match="text()"/> 
</xsl:stylesheet> 
+0

이 태그 와 소스 XML 여러 노드이며, I는를 찾아야 : 여기

는 1 결과를 출력하고, 파일에 초 결과를 쓰는 XSLT 2.0 변환이고 특정 텍스트 "일부 텍스트"가있는 노드 노드. 이 노드 만 추출하고이 특정 텍스트가 "일부 텍스트"이고 다른 노드가 이 아닌 노드를 추출하려면 어떻게합니까? – user840930

+0

@ user840930 : 질문을 편집하고 모든 관련 정보를 제공해주십시오. 다음 번에는 질문을 좀 더 세 심하게 배웁니다. 귀하의 의견을 반영하도록 답변을 업데이트했습니다. –