2012-04-12 6 views
3

를 사용하여 속성 나는 다음과 같은 소스 XML이 있습니다및 XSLT

<SampleRequest xmlns="http://sample.com/s" 
     xmlns:s1="http://sample.com/s1" 
     xmlns:s2="http://sample.com/s2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://sample.com/s schema.xsd"> 
<data> 
<s1:customer s1:firstName="Jim" s1:lastName="Ellison"/> 
<s2:address> 
    123 test street 
</s2:address> 
</data> 
</SampleRequest> 

나는 다음과 같은

<SampleRequest xmlns="http://sample.com/t" 
     xmlns:t1="http://sample.com/t1" 
     xmlns:t2="http://sample.com/t2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://sample.com/t targetSchema.xsd"> 
<data> 
<t1:customer t1:firstName="Jim" t1:lastName="Ellison"/> 
<t2:address> 
    123 test street 
</t2:address> 
</data> 
</SampleRequest> 

모두 XMLS로 변환 할 필요가 동일한 스키마를 가지고 있지만 다른 네임 스페이스.

나는

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns="http://sample.com/s" 
      xmlns:s1="http://sample.com/s1" 
      xmlns:s2="http://sample.com/s2" 
      xmlns:t1="http://sample.com/t1" 
      xmlns:t2="http://sample.com/t2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="s1 s2"> 
<xsl:output method="xml" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:template match="/"> 
    <SampleRequest xmlns="http://sample.com/t" 
     xmlns:t1="http://sample.com/t1" 
     xmlns:t2="http://sample.com/t2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <xsl:attribute name="xsi:schemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance"> 
      http://sample.com/t targetSchema.xsd 
     </xsl:attribute> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </SampleRequest> 
</xsl:template> 
<xsl:template match="s1:*"> 
    <xsl:element name="t1:{local-name()}" namespace="http://sample.com/t1">  
     <xsl:apply-templates select="* | node()"/> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="s2:*"> 
    <xsl:element name="t2:{local-name()}" namespace="http://sample.com/t2"> 
     <xsl:apply-templates select="* | node()"/> 
    </xsl:element> 
</xsl:template> 
</xsl:stylesheet> 

XSLT 다음 사용하려고하고 있었고, 난 그 결과

<?xml version="1.0" encoding="UTF-8"?><SampleRequest xmlns="http://sample.com/t" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t2="http://sample.com/t2" xmlns:t1="http://sample.com/t1" xsi:schemaLocation=" http://sample.com/t targetSchema.xsd"> 
<t1:customer/> 
<t2:address> 
    123 test street 
</t2:address> 
</SampleRequest> 

다음 얻고 모든 속성이없는 것 같다. 나는 XSLT에 매우 익숙하다. 모든 도움을 주었다.

답변

7

XSLT에는 두 가지 문제점이있다. 첫째, s1 및 s2 요소와 일치하는 곳에서는 이후에 모든 특성에 템플릿을 적용하려고하지 않습니다. 당신은 두 번째로, 당신이 그런 속성과 일치하는 모든 템플릿이없는이 문장

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

이 문

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

를 교체해야합니다. 당신은 템플릿과 같이해야합니다 :

<SampleRequest 
    xsi:schemaLocation="http://sample.com/t targetSchema.xsd" 
    xmlns:t1="http://sample.com/t1" 
    xmlns:t2="http://sample.com/t2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://sample.com/t"> 
    <t1:customer t1:firstName="Jim" t1:lastName="Ellison"/> 
    <t2:address> 123 test street </t2:address> 
</SampleRequest> 
:

<xsl:template match="@s1:*"> 
    <xsl:attribute name="t1:{local-name()}"> 
     <xsl:value-of select="." /> 
    </xsl:attribute> 
</xsl:template> 

이 XSLT

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://sample.com/s" 
    xmlns:s1="http://sample.com/s1" 
    xmlns:s2="http://sample.com/s2" 
    xmlns:t1="http://sample.com/t1" 
    xmlns:t2="http://sample.com/t2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="s1 s2"> 

    <xsl:output method="xml" encoding="UTF-8" indent="yes"/> 

    <xsl:strip-space elements="*"/> 

    <xsl:template match="/"> 
     <SampleRequest 
     xmlns="http://sample.com/t" 
     xmlns:t1="http://sample.com/t1" 
     xmlns:t2="http://sample.com/t2" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <xsl:attribute name="xsi:schemaLocation">http://sample.com/t targetSchema.xsd</xsl:attribute> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
     </SampleRequest> 
    </xsl:template> 

    <xsl:template match="s1:*"> 
     <xsl:element name="t1:{local-name()}"> 
     <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="@s1:*"> 
     <xsl:attribute name="t1:{local-name()}"> 
     <xsl:value-of select="."/> 
     </xsl:attribute> 
    </xsl:template> 

    <xsl:template match="s2:*"> 
     <xsl:element name="t2:{local-name()}"> 
     <xsl:apply-templates select="@* | node()"/> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="@s2:*"> 
     <xsl:attribute name="t2:{local-name()}"> 
     <xsl:value-of select="."/> 
     </xsl:attribute> 
    </xsl:template> 
</xsl:stylesheet> 

을 시도 샘플 XML에 적용하면 다음이 출력됩니다