2011-12-18 2 views
2

XSLT에 정의 된 xml 파일에 템플릿 이름을 적용해야하는 C#으로 작성된 응용 프로그램이 있습니다.XSLT 템플릿 선택

예 XML :

<Data> 
    <Person> 
     <Name>bob</Name> 
     <Age>43</Age> 
    </Person> 
    <Thing> 
     <Color>Red</Color> 
    </Thing> 
</Data> 

예 XSLT는 :

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> 

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

    <xsl:param name="TargetName" /> 
    <xsl:param name="RootPath" /> 

    <xsl:Template Name="AgeGrabber"> 
     <xsl:value-of select="/Person/Age" /> 
    </xsl:Template> 

    <xsl:Template Name="ColorGrabber"> 
     <xsl:value-of select="/Color" /> 
    </xsl:Template> 

</xsl:stylesheet> 

내가 "/ 데이터/것"경로 템플릿 "ColorGrabber"를 실행하고 싶어하고 또 다른 템플릿 변환을 실행 말 "AgeGrabber"경로 "/ Data". 이것이 가능한가? 나는 경로와 템플릿 이름 (맨 위에 2 개의 매개 변수를 포함)을 전달할 수 있다고 생각하고 스위치 유형을 수행했지만 xsl : call-template은 매개 변수를 name 속성으로 사용할 수 없습니다.

이 동작을 어떻게 수행 할 수 있습니까?

+1

변환의 예상 출력은 정확히 무엇입니까? – mzjn

답변

1

이 질문에 문제가 있습니다 :

  1. <xsl:stylesheet version="2.0" ...가 지정되고, 그러나 현재> NET은 기본적으로 XSLT 2.0을 지원하지 않습니다. 하나의 XML 문서를 모두 /Person/Age/Color 요소를 포함 할 수 없기 때문에

  2. Thecode의 예는, 너무 의미가 없다는 - wellformed XML 문서는 하나의 상위 요소를 가지고 있으며 Person 또는 Color 있지만 둘 중 하나가 될 수 있습니다.

    <Person> 
    <Age>27</Age> 
    <HairColor>blond</HairColor> 
    </Person> 
    

    간단한 솔루션이다 :

는 경우에 더 의미 예 있었다

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:param name="pProperty" select="'Age'"/> 

<xsl:template match="/"> 
    <xsl:value-of select="/*/*[name()=$pProperty]"/> 
</xsl:template> 
</xsl:stylesheet> 

이 변환은 상기 XML 문서에 적용될 때 원하는 결과를 얻습니다.

27 

51,515,경우 관심있는 요소의 nestedness 임의의 수 및/또는 우리가 다른 요소에 다른 처리를 수행해야하고, 적절한 용액 템플릿 (없다라는 것)을 일치 사용하는 것이다 : 이러한 변화는 동일한 XML 문서 (위)에인가

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pProperty" select="'HairColor'"/> 

<xsl:template match="Age"> 
    <xsl:if test="$pProperty = 'Age'"> 
    This person is <xsl:value-of select="."/> old. 
    </xsl:if> 
</xsl:template> 

<xsl:template match="HairColor"> 
    <xsl:if test="$pProperty = 'HairColor'"> 
    This person has <xsl:value-of select="."/> hair. 
    </xsl:if> 
</xsl:template> 
</xsl:stylesheet> 

는 다시 정확한 결과 제조된다 :

This person has blond hair. 
,745,151 당신이 정말로 XSLT 1.0 XSLT 2.0 higher-order 함수 (HOF)를 시뮬레이션하려는 경우 마지막으로

는이 대답을 참조하십시오 https://stackoverflow.com/a/8363249/36305을, 또는 약 FXSL을 배웁니다.

0

훨씬 간단 : (나이와 컬러 요소)이 적용되는 템플릿 규칙을 준비하고 조건부로 변환하기 위해 적절한 노드 보내 -//사람/연령//것/색상

0

당신은 뒤로 그것을 가지고 있습니다. 사용할 노드와 일치하는 템플릿을 만들어야합니다.

<xsl:stylesheet> 
    <xsl:template match="Person|Thing"> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="Person"> 
     <xsl:value-of select="Age" /> 
    </xsl:template> 

    <xsl:template match="Thing"> 
     <xsl:value-of select="Color" /> 
    </xsl:template> 

</xsl:stylesheet>