2012-03-08 3 views
1

나는이처럼 보이는 XML 파일이있는 경우 :XSLT를 사용하여 XML에 반복 요소 이름을 추가 하시겠습니까?

<properties> 
    <property> 
     <picture1>http://example.com/image1.jpg</picture1> 
     <picture2>http://example.com/image2.jpg</picture2> 
     <picture3>http://example.com/image3.jpg</picture3> 
     <picture4>http://example.com/image4.jpg</picture4> 
     <picture5>http://example.com/image5.jpg</picture5> 
    </property> 
</properties> 

내가 가정에서 수정 건가요 : 나는 각 사진의 URL 요소가이 같은 독특한 곳으로 변환 가겠어요 어떻게

<properties> 
    <property> 
     <picture>http://example.com/image1.jpg</picture> 
     <picture>http://example.com/image2.jpg</picture> 
     <picture>http://example.com/image3.jpg</picture> 
     <picture>http://example.com/image4.jpg</picture> 
     <picture>http://example.com/image5.jpg</picture> 
    </property> 
</properties> 

을 요소 중 일부에 빈 값이 포함되어 있어도 요소 당 동일한 양이 있어야합니다 (그림 URL의 개수는 속성에 따라 다릅니다).

+0

단어가 누락 된 것처럼 보입니다. "____ 개 당 같은 양의 요소가 있어야합니다 ..." – LarsH

답변

2

이 짧고 간단 변환 (사용없이 축)

<properties> 
    <property> 
     <picture>http://example.com/image1.jpg</picture> 
     <picture>http://example.com/image2.jpg</picture> 
     <picture>http://example.com/image3.jpg</picture> 
     <picture>http://example.com/image4.jpg</picture> 
     <picture>http://example.com/image5.jpg</picture> 
    </property> 
</properties> 
: 제공된 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="picture"> 
    <xsl:element name="picture{position()}"> 
    <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

이 원하는, 올바른 결과을 생성합니다

<properties> 
    <property> 
     <picture1>http://example.com/image1.jpg</picture1> 
     <picture2>http://example.com/image2.jpg</picture2> 
     <picture3>http://example.com/image3.jpg</picture3> 
     <picture4>http://example.com/image4.jpg</picture4> 
     <picture5>http://example.com/image5.jpg</picture5> 
    </property> 
</properties> 

설명 :

  1. picture 요소의 identity rule 재정. AVTxsl:elementname 속성 내의 position() 기능 사용

  2. .

  3. xsl:strip-space의 용도.

3

count(preceding-sibling::*)+1을 사용하면 현재 요소의 색인을 가져올 수 있습니다.

완료 예 :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<!-- identity transform --> 
<xsl:template match="node()|@*"> 
<xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy> 
</xsl:template> 

<!-- override for picture elements to rename element --> 
<xsl:template match="picture"> 
    <xsl:element name="{name()}{count(preceding-sibling::*)+1}"> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 
+0

완벽한, 감사합니다! – Kurt

+0

완벽한, 감사합니다! – Kurt

+0

@ Kurt,'count (preceding-sibling :: *) + 1' 대신'position()'을 사용할 수도 있습니다. 프란시스? – LarsH

관련 문제