2012-09-07 2 views
0

다음 요소를 지역화 할 수있는 값 (기본적으로 영어 구)을 제외하고 동일한 요소와 속성을 가진 XML로 변환해야합니다.XSL은 요소 및 특성 값만 필터링하는 XML을 변환합니까?

일부 요소 (<footnote>)와 속성은 선택 사항이며 (<display_data_type>), 각 요소에 대한 템플릿이 없어도이를 일반적으로 수행하고 싶습니다. 그게 가능하니?

궁극적 인 목표는 지역화 된 문자열을 무시하고 XML의 기본 버전을 현지화 된 버전과 비교할 수 있도록하는 것입니다. 예를 들어

다음

<data_schema> 
    <field symbol="ACCOUNT" type="string" name="Account Number"> 
     <validators> 
      <maxlength>6</maxlength> 
     </validators> 
     <description>The account number</description> 
     <example>123456</example> 
     <default_value></default_value> 
    </field> 
    <field symbol="POSTAL_CODE" type="string" name="Postal Code"> 
     <description>Postal Code for account</description> 
     <example>22022</example> 
     <footnote>Does not apply to certain accounts</footnote> 
     <default_value></default_value> 
    </field> 
    <field symbol="DISCOUNT" type="string" name="Discount Percentage" display_data_type="percentage"> 
     <description>Descount determined by account</description> 
     <example>1.5%</example> 
     <default_value></default_value> 
    </field> 
</data_schema> 

는로 변환 할 것이다 :

<data_schema> 
    <field symbol="ACCOUNT" type="string" name=""> 
     <validators> 
      <maxlength>6</maxlength> 
     </validators> 
     <description/> 
     <example/> 
     <default_value/> 
    </field> 
    <field symbol="POSTAL_CODE" type="string" name=""> 
     <description/> 
     <example/> 
     <footnote/> 
     <default_value/> 
    </field> 
    <field symbol="DISCOUNT" type="string" name="" display_data_type="percentage"> 
     <description/> 
     <example/> 
     <default_value/> 
    </field> 
</data_schema> 
+0

지역화 할 수있는 질문은 중요하지 않습니다. 요소 나 속성을 제외한 모든 예가 허용됩니다. 그리고 문제가된다면 어떤 특정 요소가 지역화 가능한지를 보여주는 예제 입력과 출력. – Glenn

답변

1

여기 예입니다. 이와 같은 템플릿을 적용하면 트리가 아닌 텍스트 영역과 심볼 또는 유형이 아닌 속성의 속성 텍스트가 생성되어야합니다.

<xsl:template match="*"> 
    <xsl:element name="{name()}"> 
    <xsl:for-each select="@*"> 
     <xsl:choose> 
     <xsl:when test="name() = 'symbol' or name() = 'type'"> 
      <xsl:copy-of select="."/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:attribute name="{name()}"/> 
     </xsl:otherwise> 
     </xsl:choose> 
    </xsl:for-each> 

    <xsl:apply-templates select="*"/> 
    </xsl:element> 
</xsl:template> 
+0

정확히 내가 필요로하는 것. 감사! – Glenn

+1

잘못되었습니다. 분명히 답변 포스터 나 OP가이 솔루션을 테스트하지 않았습니다. –

+0

숀, 당신이 틀렸어 -이 작품은 내 목적에 충분하다. 다시 시도하십시오. – Glenn

4

다른 접근법이 있습니다. 이것은 기본적으로 모든 노드를 그대로 복사하는 "Identity Transform"의 XSLT 디자인 패턴을 기반으로합니다. 당신은 다른 템플릿을 추가하려면이 옵션을 확장 할

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

는 귀하의 경우 텍스트를 제거하는 것입니다 특정 작업을 수행 할 노드에 맞게 일치합니다. 필요한 템플릿은 변환의 정확한 규칙에 따라 달라집니다.

<xsl:template match="description|example|footnote|default_value"> 
    <xsl:copy/> 
</xsl:template> 

<xsl:template match="@name"> 
    <xsl:attribute name="{name()}"/> 
</xsl:template> 

그래서,이 경우의 요소 설명 :

당신은 당신이 다음과 같은 두 가지 템플릿을 추가 할 노드를 복사 할 수 있지만 텍스트없이 특정 요소와 속성의 텍스트를 제거 할해야 , , 각주default_value은 텍스트가 제거 된 후 @name 속성과 함께 삭제됩니다. 다른 모든 노드는 텍스트와 함께 그대로 복사됩니다. 당신이 요소의 특정 목록을 가지고 있고 당신이 변경하려는 속성 otherhand,에

, 당신은, 그래서 그렇게

<xsl:template match="field/*[not(self::validators)]"> 
    <xsl:copy/> 
</xsl:template> 

<xsl:template match="@symbol|@type|@display_data_type"> 
    <xsl:copy/> 
</xsl:template> 

<xsl:template match="@*"> 
    <xsl:attribute name="{name()}"/> 
</xsl:template> 

같은 템플릿을 추가 할 수 있습니다 요소가 실제로 말을하는 검증을 위해 제거 유효성 검사기가 아닌 모든 것은 요소입니다. 유효성 검사기 요소는 ID 변환 템플릿으로 복사됩니다. 속성의 경우 유지할 속성이 명시 적으로 나열된 약간 다른 접근 방식을 보여줬고 두 번째 템플리트를 사용하여 다른 모든 템플리트를 제거했습니다.

이 경우 두 개의 전체 XSLT가 있습니다.

특정 노드

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="description|example|footnote|default_value"> 
     <xsl:copy/> 
    </xsl:template> 

    <xsl:template match="@name"> 
     <xsl:attribute name="{name()}"/> 
    </xsl:template> 

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

특정 노드

샘플 문서의 경우
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="field/*[not(self::validators)]"> 
     <xsl:copy/> 
    </xsl:template> 

    <xsl:template match="@symbol|@type|@display_data_type"> 
     <xsl:copy/> 
    </xsl:template> 

    <xsl:template match="@*"> 
     <xsl:attribute name="{name()}"/> 
    </xsl:template> 

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

의 텍스트를 유지하기위한 두 번째를 텍스트를 제거하기위한 첫 번째는 모두 동일한 출력을 생성해야합니다

<data_schema> 
    <field symbol="ACCOUNT" type="string" name=""> 
     <validators> 
     <maxlength>6</maxlength> 
     </validators> 
     <description/> 
     <example/> 
     <default_value/> 
    </field> 
    <field symbol="POSTAL_CODE" type="string" name=""> 
     <description/> 
     <example/> 
     <footnote/> 
     <default_value/> 
    </field> 
    <field symbol="DISCOUNT" type="string" name="" display_data_type="percentage"> 
     <description/> 
     <example/> 
     <default_value/> 
    </field> 
</data_schema> 
+0

감사합니다. Tim. 매우 도움이되는 설명. 주요 목표는 각 지역화 가능 속성/요소에 대해 별도의 템플리트를 제공 할 필요없이 가장 간결하고 이해하기 쉬운 xslt를 제공하는 것이 었습니다. – Glenn

관련 문제