2012-06-27 8 views
1

"a"태그의 속성을 가져와 처리해야합니다.속성을 처리하고 문자열을 기준으로 분할합니다.

출처 :

<Data> 
    <AAA> 
    <strong xmlns="http://www.w3.org/1999/xhtml">some Text 
    <a href="#" name="Value1,Value2,Value3,Value4,Value5,Value6" id="Functionaldata" xmlns="http://www.w3.org/1999/xhtml">Value6</a> 
    </strong> 
    hello 
    <a title="google" href="http://google.com" xmlns="http://www.w3.org/1999/xhtml">Hey</a> all <a href="#" name="element1,element2,element3,element4,element5,element6" id="Functionaldata" xmlns="http://www.w3.org/1999/xhtml">element6</a> 
    <AAA> 
</Data> 

출력

<Content> 
    <Information> 
    <text> 
     <strong xmlns="http://www.w3.org/1999/xhtml">some Text 
     <dynamicinfo type="Value1" name="Value2" group="Value3" id="Value4" link="Value5" display="Value6"/> 
    </strong> 
    hello<a title="google" href="http://google.com">Hey</a> all 
    <dynamicinfo type="element1" name="element2" group="element3" id="element4" link="element5" display="element6"/> 
    </text> 
    </Information> 
</Content> 

나는 ID = Functionaldata와 "A"태그 처리에 강타하고있다.

누구나 의견을 제시 할 수 있습니까?

감사합니다.

+0

사용자 222 : 당신에게 내 대답은 도움이 되었습니까 당신은 여전히해야합니까 어떤 문제? –

+0

@DimitreNovatchev. 그것은 나에게 유용하다. 감사합니다. – Patan

+0

222 : 환영합니다. –

답변

1

이 변환 :

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

    <my:names> 
     <n>type</n> 
     <n>name</n> 
     <n>group</n> 
     <n>id</n> 
     <n>link</n> 
     <n>display</n> 
    </my:names> 

    <xsl:variable name="vNames" select="document('')/*/my:names/*"/> 

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

     <xsl:template match="a[@name] | x:a[@name]"> 
      <xsl:copy> 
      <xsl:apply-templates select="@*[not(name()='name')]"/> 
      <xsl:apply-templates select="@name"/> 
      </xsl:copy> 
     </xsl:template> 

    <xsl:template match="a/@name | x:a/@name" name="split"> 
     <xsl:param name="pText" select="."/> 
     <xsl:param name="pOrd" select="1"/> 

     <xsl:if test="$pText"> 
     <xsl:attribute name="{$vNames[position()=$pOrd]}"> 
      <xsl:value-of select= 
      "substring-before(concat($pText, ','), ',')"/> 
     </xsl:attribute> 

     <xsl:call-template name="split"> 
      <xsl:with-param name="pText" select="substring-after($pText, ',')"/> 
      <xsl:with-param name="pOrd" select="$pOrd+1"/> 
     </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 
제공된 XML 문서에 적용

:

<Data> 
    <AAA> 
     <strong xmlns="http://www.w3.org/1999/xhtml">some Text 
     <a href="#" id="Value4" type="Value1" name="Value2" group="Value3" link="Value5" display="Value6"/> 
     </strong> 
     hello 
     <a title="google" href="http://google.com">Hey</a> all 
     <a href="#" id="element4" type="element1" name="element2" group="element3" link="element5" display="element6"/> 
    </AAA> 
</Data> 
:

<Data> 
     <AAA> 
     <strong xmlns="http://www.w3.org/1999/xhtml">some Text 
     <a href="#" name="Value1,Value2,Value3,Value4,Value5,Value6" id="Functionaldata">Value6</a> 
     </strong> 
     hello 
     <a title="google" href="http://google.com">Hey</a> all 
     <a href="#" name="element1,element2,element3,element4,element5,element6" id="Functionaldata">element6</a> 
     </AAA> 
</Data> 

는 원하는 정확한 결과를 생성 17,451,515,

설명는 :

이 다음과 같이 변경하여, 이전 질문의 솔루션을 기반으로 구축 :

  1. 사용 및 신원 규칙의 재정의를.

  2. 요소 대신 속성 값이 분할 결과로 생성됩니다.

  3. 생성 될 속성의 이름은 전역 my:names 요소의 하위로 지정됩니다.

UPDATE : 영업 이익은 자신의 질문을 수정 한 코멘트에서

말은 :

나는 ""태그의 이름 공간을 추가하는 것을 잊었다. 모든 "a"태그에는 "xhtml"네임 스페이스가 있습니다.

대답은이 경우 제공된 변환이 여전히 정상적으로 작동하며 변경이 필요 없다는 것입니다.

그러나 다음과 같은 방법으로 간단하게 할 수있다 :

교체 :

<xsl:template match="a[@name] | x:a[@name]"> 

단지로 :

<xsl:template match="x:a[@name]"> 

교체 : 단지

<xsl:template match="a/@name | x:a/@name" name="split"> 

:

<xsl:template match="x:a/@name" name="split"> 

이러한 변경 후 전체 변환된다 :

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

    <my:names> 
     <n>type</n> 
     <n>name</n> 
     <n>group</n> 
     <n>id</n> 
     <n>link</n> 
     <n>display</n> 
    </my:names> 

    <xsl:variable name="vNames" select="document('')/*/my:names/*"/> 

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

     <xsl:template match="x:a[@name]"> 
      <xsl:copy> 
      <xsl:apply-templates select="@*[not(name()='name')]"/> 
      <xsl:apply-templates select="@name"/> 
      </xsl:copy> 
     </xsl:template> 

    <xsl:template match="x:a/@name" name="split"> 
     <xsl:param name="pText" select="."/> 
     <xsl:param name="pOrd" select="1"/> 

     <xsl:if test="$pText"> 
     <xsl:attribute name="{$vNames[position()=$pOrd]}"> 
      <xsl:value-of select= 
      "substring-before(concat($pText, ','), ',')"/> 
     </xsl:attribute> 

     <xsl:call-template name="split"> 
      <xsl:with-param name="pText" select="substring-after($pText, ',')"/> 
      <xsl:with-param name="pOrd" select="$pOrd+1"/> 
     </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 
+0

해답을 가져 주셔서 감사합니다. 작은 의심, "a"태그에 이름 공간을 추가하는 것을 잊었습니다. 모든 "a"태그에는 "xhtml"네임 스페이스가 있습니다. 그런 다음 코드가 변경됩니다. – Patan

+0

@ User222 : 모든 'a'요소가 xhtml 네임 스페이스에 속하면 변환을 변경할 필요가 없습니다. 그것은 단순화 될 수 있으므로 일치 패턴'a/@ name | x : a/@ name'은 단지'x : a/@ name'이됩니다. 독자가 혼동되지 않도록 소스 XML 문서의 모든 'a' 요소가 실제로 xhtml 네임 스페이스에 있도록 질문을 편집하십시오. –

+0

@ User222 : 내 대답의 업데이트를 참조하십시오. –

0

EXSLT에 액세스 할 수있는 경우 str:split() 메서드를 사용하면됩니다.

더 큰 질문은 어떻게 값을 지정한 새로운 속성 이름에 매핑 할 것인가입니다. 나는 선행 값 이름을 선언 한 후 분할 값 1, 분할 값 2 값 이름이 등에 값 이름 하나를 매핑하는이 함께했다

당신은 this XMLPlayground session에서 행동에서 볼 수
<!-- node-set of value names, which will each correspond to the value at the same index, once they're split --> 
<xsl:variable name='value_names' select='str:split("type|name|group|id|link|display", "|")' /> 

<!-- match root --> 
<xsl:template match="/"> 
    <dynamicdata> 
     <xsl:apply-templates name='values' select='str:split(root/a/@name, ",")' /> 
    </dynamicdata> 
</xsl:template> 

<!-- iteration content - each value from the split --> 
<xsl:template match='token'> 
    <xsl:variable name='pos' select='position()' /> 
    <xsl:attribute name='{$value_names[$pos]}'><xsl:value-of select='.' /></xsl:attribute> 
</xsl:template> 

(참조 출력 소스).

관련 문제