2010-06-06 4 views
2

XML을 변환하기 위해 XSLT를 사용하려고합니다. 다음과 같이 샘플 XML이다 : 나는 "노트"모든 "참고"요소를 추출하고 부모 노드 아래에 그 (것)들이 찾고 있어요XSLT를 사용하여 XML 노드 재구성

<root> 
<info> 
    <firstname>Bob</firstname> 
    <lastname>Joe</lastname> 
</info> 
<notes> 
    <note>text1</note> 
    <note>text2</note> 
</notes> 
<othernotes> 
    <note>text3</note> 
    <note>text4</note> 
</othernotes> 

.

다음과 같이 내가 찾고 그 결과는 다음과 같습니다 어떻게 내가 할 수있는

<root> 
<info> 
    <firstname>Bob</firstname> 
    <lastname>Joe</lastname> 
</info> 
<notes> 
    <note>text1</note> 
    <note>text2</note> 
    <note>text3</note> 
    <note>text4</note> 
</notes> 
</root> 

내가 나를 내 모든 "노트"를 추출 할 수 있도록되어 사용 시도한 XSLT를, 그러나, 나는 알아낼 수 없습니다 "노트"노드에서 다시 래핑하십시오.

<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="notes|othernotes"> 
     <xsl:apply-templates select="note"/> 
    </xsl:template> 
    <xsl:template match="*"> 
    <xsl:copy><xsl:apply-templates/></xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

나는 위의 XSLT와지고있어 그 결과는 다음과 같습니다 :

<root> 
<info> 
    <firstname>Bob</firstname> 
    <lastname>Joe</lastname> 
</info> 
    <note>text1</note> 
    <note>text2</note> 
    <note>text3</note> 
    <note>text4</note> 
</root> 

감사

답변

2

당신은이 같은 요소를 생성 할 수 있습니다 여기에

은 내가 사용 XSLT입니다 :

<xsl:element name="notes"> 
    <!-- inject content of notes element here using e.g. <xsl:copy> or <xsl:copy-of> --> 
</xsl:element> 

약간의 수정을 통해 위의 방법은 특정 XML 네임 스페이스의 요소를 생성하는데도 사용됩니다. 당신이 찾고있을거야

<xsl:template match="root"> 
    <root> 
    <xsl:copy-of select="info"/> 
    <notes> 
     <xsl:copy-of select="*/note"/> 
    </notes> 
    </root> 
</xsl:template> 
+1

니스 깨끗하고 +1. 나는 을 재현하는 대신에 을 사용 하겠지만, 나는 개인적인 취향의 문제라고 생각한다. 또한 특정 네임 스페이스에 요소를 만들려면 접두어를 쉽게 선언하고 ''을 사용하면됩니다. 요소 이름이 동적이어야한다면''이 정말로 필요합니다. – Tomalak

1

: 나는 다음을 수행 할 스타일 시트를 재구성 할 특정 예에서

<notes> 
    <!-- inject content of notes element here using e.g. <xsl:copy> or <xsl:copy-of> --> 
</notes> 

: 그러나 당신이 네임 스페이스의 요소를 생성하기 위해 찾고 있지 않기 때문에 바로 가기가 존재 -

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

<xsl:template match="/root"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()[local-name() != 'notes' and local-name() != 'othernotes'] 
    </xsl:copy> 
    <notes> 
    <xsl:apply-templates select="othernotes/note | notes/note" /> 
    </notes> 
</xsl:template> 

루트 노드의 구조를 제어합니다. 먼저 "notes"또는 "othernote"로 이름이 지정되지 않은 루트 아래의 모든 내용을 복사하십시오. 그런 다음 "notes"요소를 직접 만든 다음 "othernotes"또는 "notes"요소 아래에있는 모든 "note"요소를 결합합니다.

+0

'select = "@ * | node() [self (주 : self 또는 othernotes)]" ";--) (이 예제에서'select ="info "') – Tomalak

+0

@Tomalak : 사실 나는 그걸 가지고 있었지만 local-name을 사용하기로 되돌려 놓았습니다. 왜 지금 생각할 수 없습니까. @ user268396의 솔루션은 매우 구체적인 예에서 작동합니다. 최소한의 가정을 수행했습니다. (정보에 대한 다른 형제 자매가있을 수 있으며,이 병합과 관련없는 노트 요소가 정보에 포함될 수 있습니다.) – AnthonyWJones

+0

그래서 +1을 해결책. 나는 비슷한 것을 준비했지만 당신은 더 빨랐다. 그래서 나는 그것을 게시하지 않았다. :) – Tomalak

관련 문제