2011-09-16 6 views
4

XSLT를 사용하여 XML 문서를 수정하는 방법이 있는지 또는 XSLT보다 더 좋은 방법이 있습니까?XSLT를 사용하여 xml 문서 수정

<feed xmlns="http://www.w3.org/2005/Atom"> 
<id>http://libx.org/libx2/libapps</id> 
<entry> 
<id>http://libx.org/libx2/libapps/2</id> 
</entry> 
<entry> 
<id>http://libx.org/libx2/libapps/3</id> 
</entry> 
</feed> 

나는 다음과 같은 작업을 수행하고 싶습니다 : 이드 (공급의 텍스트 제거 : ID)를

  1. 피드를 수정을
  2. 말은, 나는 XML과 같이가

  3. 항목 수정 : "/"다음의 마지막 숫자 값이 유지되도록 id 값을 유지합니다.

결과 XML은 다음처럼 보일 것이다 :

<feed xmlns="http://www.w3.org/2005/Atom"> 
<id></id> 
<entry> 
<id>2</id> 
</entry> 
<entry> 
<id>3</id> 
</entry> 
</feed> 

감사합니다, 소니

+0

좋은 질문, +1. 예, XSLT 1.0에서는 쉽게, XSLT 2.0에서는 간단합니다. –

답변

4

I. XSLT 1.0 솔루션 :

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <id>http://libx.org/libx2/libapps</id> 
    <entry> 
     <id>http://libx.org/libx2/libapps/2</id> 
    </entry> 
    <entry> 
     <id>http://libx.org/libx2/libapps/3</id> 
    </entry> 
</feed> 

를 : 제공된 XML 문서에 적용 할 때

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://www.w3.org/2005/Atom"> 
<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="x:feed/x:id/node()"/> 

<xsl:template match="x:entry/x:id/text()" name="eatSlashes"> 
    <xsl:param name="pText" select="."/> 

    <xsl:choose> 
    <xsl:when test="not(contains($pText, '/'))"> 
    <xsl:value-of select="$pText"/> 
    </xsl:when> 
    <xsl:otherwise> 
    <xsl:call-template name="eatSlashes"> 
    <xsl:with-param name="pText" select= 
        "substring-after($pText, '/')"/> 
    </xsl:call-template> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

: 공통의 시작 문자열을 haing 모든 URL에 대한 가정을하지 않고 모든 URL과 함께 작동이 XSLT 1.0 변환 원하는 결과를 얻으려면 :

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <id/> 
    <entry> 
     <id>2</id> 
    </entry> 
    <entry> 
     <id>3</id> 
    </entry> 
</feed> 

II. XSLT 2.0 솔루션 : 같은 XML 문서 (위)에 적용될 때

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:x="http://www.w3.org/2005/Atom"> 
<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="x:feed/x:id/node()"/> 

<xsl:template match="x:entry/x:id/text()"> 
    <xsl:sequence select="tokenize(.,'/')[last()]"/> 
</xsl:template> 
</xsl:stylesheet> 

이 같은 올바른 결과가을 생산 :

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <id/> 
    <entry> 
     <id>2</id> 
    </entry> 
    <entry> 
     <id>3</id> 
    </entry> 
</feed> 
0

XSLT 내 지식은 최고는 아니지만이 작동하는 것 같다 :

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

    <xsl:template match="a:feed/a:id" xmlns:a="http://www.w3.org/2005/Atom"> 
    <xsl:copy/> 
    </xsl:template> 
    <xsl:template match="a:entry/a:id" xmlns:a="http://www.w3.org/2005/Atom"> 
    <xsl:copy><xsl:value-of select="substring-after(.,'http://libx.org/libx2/libapps/')"/></xsl:copy> 
    </xsl:template> 

</xsl:stylesheet>