2009-04-10 5 views
2

원본 XML에서 일부 요소 이름을 변경해야합니다. XSLT를 사용하여이 작업을 수행하려하지만 작동하지 못합니다. 나는 그것을 변경해야XSLT를 사용하여 XML 요소 이름 바꾸기

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet href="test.xsl" type="text/xsl"?> 
<html> 
<body> 
    <section>Jabber</section>  
      <itemtitle>JabberJabber</itemtitle> 
        <p>Always Jabber Jabber Jabber</p> 
      <h3>Emboldened Requests </h3> 
        <p>Somemore Jabber Here</p> 
        <img scr="bigpicture.jpg"></img> 
      <poll><p>Which statement best characterizes you?</p></poll> 
      <pcredit>Left: Jumpin Jasper/Jumpy Images</pcredit> 
</body> 
</html> 

: 여기

는 XML의 샘플입니다 여기에
<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet href="test.xsl" type="text/xsl"?> 
<html> 
<body> 
    <div class="issuehead">Jabber</div> 
    <div class="issuetitle">JabberJabber</div> 
     <p>Always Jabber Jabber Jabber</p> 
    <h3>Emboldened Requests </h3> 
     <p>Somemore Jabber Here</p> 
    <img scr="bigpicture.jpg"></img> 
    <div class="poll"><p>Which statement best characterizes you?</p></div> 
    <div class="pcredit">Left: Jumpin Jasper/Jumpy Images</div> 
</body> 
</html> 

내가했던 XSLT이지만, 나는 그것이 동작하지 않습니다 :

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

<xsl:output method="html" /> 

<xsl:template match="/"> 

<html> 
<head> 
</head> 
<body> 
    <xsl:apply-templates/> 
</body>  
</html> 

    <xsl:template match="section"> 
    <div class="issuehead"><xsl:value-of select="."/></div> 
    </xsl:template> 

    <xsl:template match="itemtitle"> 
    <div class="issuetitle"> <xsl:value-of select="."/></div> 
    </xsl:template> 

    <xsl:template match="img"></xsl:template> 

    <xsl:template match="poll"> 
    <div class="poll"><xsl:value-of select="."/></div> 
    </xsl:template> 

    <xsl:template match="pcredit"> 
    <div class="pcredit"><xsl:value-of select="."/></div> 
    </xsl:template> 

    <xsl:template match="p"></xsl:template> 

    <xsl:template match="h3"></xsl:template> 

</xsl:template> 
</xsl:stylesheet> 

감사합니다.

+2

이가이다 심하게 공식화 된 질문의 예. 제목에는 "XML 요소 이름 바꾸기"라고되어 있지만 제공된 소스 XML 문서에서 필요한 결과로 요소를 명확하게 이름을 바꿀 수는 없습니다. 또한 내용에 변화가있는 것으로 보이며 이러한 변경에 대한 규칙은 전혀 설명되지 않습니다. -1 ! –

+0

음, 맞아. 이 "나쁜 질문"에 태그를 지정하는 방식으로 상황이 개선되는 것을 나는 볼 수 없으므로 태그를 되돌릴 것입니다. – Jonik

답변

10

이런 건 들어, 변환 ID로 시작합니다

<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:stylesheet> 

이것은 단지 모든 노드를 복사합니다. 하지만 필요한 템플릿을 추가하면됩니다.

<xsl:template match="section"> 
    <div class="issuehead"> 
     <xsl:apply-templates select="@* | node()"/> 
    </div> 
</xsl:template> 

이 패턴을 통해 원하는 것을 얻을 수 있습니다.

+0

알려진 상태에서 간단하게 시작하는 것이 보통 올바른 방법입니다. 거기에서 솔루션은 점진적으로 증가 할 수 있습니다. –

+0

선택자를 설명해 주시겠습니까? 그것은 훌륭하게 작동하지만 실제로 이해할 수는 없습니다 ... 예를 들어 node()가 요소 이름과 일치합니까? – Maarten

+0

노드와 일치합니다. 이것은 문자열 조작이 아닙니다. –

2

다음 템플릿은 당신이 찾고있는 결과를 얻을 :

더 잘 XSL 수정 마크 업을 형성하고 싶은 경우
<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="html" /> 

    <!-- Copy all elements and attributes from the source to the target --> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <!-- Transform the given elements to divs with class names matching the source element names --> 
    <xsl:template match="itemtitle|poll|pcredit"> 
     <div class="{local-name(.)}"> 
      <xsl:value-of select="."/> 
     </div> 
    </xsl:template> 

    <!-- Transform the section element to a div of class 'issuehead' --> 
    <xsl:template match="section"> 
     <div class="issuehead"> 
      <xsl:value-of select="."/> 
     </div> 
    </xsl:template> 

</xsl:stylesheet> 

다음과 같이 출력 태그 :

<xsl:output method="xml" 
    version="1.0" 
    encoding="UTF-8" 
    indent="yes" 
    omit-xml-declaration="yes" 
    doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
/> 
관련 문제