2010-05-05 3 views
5

XSLT를 배우려고하지만 예제로 가장 잘 작동합니다. 간단한 스키마 - 스키마 변환을 수행하고 싶습니다. 이 변환은 단 한번의 패스로 어떻게 수행합니까 (현재 솔루션은 두 번의 패스를 사용하고 고객의 원래 순서를 잃습니다).for for eachin XSLT에서 다중 노드를 선택하는 방법

에서 :

<?xml version="1.0" encoding="UTF-8"?> 
<sampleroot> 

<badcustomer> 
    <name>Donald</name> 
    <address>Hong Kong</address> 
    <age>72</age> 
</badcustomer> 

<goodcustomer> 
    <name>Jim</name> 
    <address>Wales</address> 
    <age>22</age> 
</goodcustomer> 

<goodcustomer> 
    <name>Albert</name> 
    <address>France</address> 
    <age>51</age> 
</goodcustomer> 

</sampleroot> 

에 :

<?xml version="1.0" encoding="UTF-8"?> 
<records> 

<record id="customer"> 
    <name>Donald</name> 
    <address>Hong Kong</address> 
    <age>72</age> 
    <customertype>bad</customertype> 
</record> 

<record id="customer"> 
    <name>Jim</name> 
    <address>Wales</address> 
    <age>22</age> 
    <customertype>good</customertype> 
</record> 

<record id="customer"> 
    <name>Albert</name> 
    <address>France</address> 
    <age>51</age> 
    <customertype>good</customertype> 
</record> 

</records> 

내가 이미이에게 나쁜 방법으로 해결 (I는 고객의 주문을 잃고 난 파일을 구문 분석 할 필요가 있다고 생각 여러 번 :

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

    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="/sampleroot"> 

    <records> 

     <xsl:for-each select="goodcustomer"> 
      <record id="customer"> 
       <name><xsl:value-of select="name" /></name> 
       <address><xsl:value-of select="address" /></address> 
       <age><xsl:value-of select="age" /></age> 
       <customertype>good</customertype> 
      </record> 
     </xsl:for-each> 

     <xsl:for-each select="badcustomer"> 
      <record id="customer"> 
       <name><xsl:value-of select="name" /></name> 
       <address><xsl:value-of select="address" /></address> 
       <age><xsl:value-of select="age" /></age> 
       <customertype>bad</customertype> 
      </record> 
     </xsl:for-each> 

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

제발 누군가가 올바른 XSLT 구조로 나를 도우 려 할 수 있습니까? 단 하나의 구문 분석 만 사용해야합니다 (각각 하나씩).

감사합니다,

크리스

+0

좋은 질문 Chris (+1). 좋은 해결책은 내 대답을 참조하십시오. :) –

답변

6

가능한만큼 <xsl:for-each>을 사용하지 않도록하는 것이 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="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

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

<xsl:template match="badcustomer | goodcustomer"> 
    <record> 
    <xsl:apply-templates/> 
    <customertype> 
    <xsl:value-of select="substring-before(name(), 'customer')"/> 
    </customertype> 
    </record> 
</xsl:template> 
</xsl:stylesheet> 

주의 마십시오

  1. 만 템플릿과 <xsl:apply-templates> 사용됩니다.

  2. 필요에 따라 신원 규칙과 그 우선 적용을 사용합니다. 이것은 가장 기본적인 XSLT 디자인 패턴 중 하나입니다.

+0

감사합니다. 마지막으로 한 가지 정보를 요청할 수 있습니까? 원본 xml 스키마의 특성을 전달하려면 xslt를 조정하여 어떻게 작동합니까? <= "의 AttributeValue"이름 myattribute> 도널드 <주소 myattribute2 = "attributevalue2"> 홍콩 72 Chris

+0

@ 크리스 : 제대로 함께, 새로운 질문으로이 문의 해주십시오 형식화 된 코드/xml 또한 "스키마"라는 단어를 잘못 사용하고 있습니다. 실제로 "소스 XML 문서"또는 해당 문서 유형의 스키마 인스턴스를 참조합니다. –

+0

"스키마"의 잘못된 사용에 대한 정보 및 수정에 대해 감사드립니다. 나는 후속 질문을 해결하는 방법을 알아 냈으므로 이제 새로운 질문을하지 않을 것이다. – Chris