이 변환 :
<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,
설명는 :
이 다음과 같이 변경하여, 이전 질문의 솔루션을 기반으로 구축 :
사용 및 신원 규칙의 재정의를.
요소 대신 속성 값이 분할 결과로 생성됩니다.
생성 될 속성의 이름은 전역 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>
사용자 222 : 당신에게 내 대답은 도움이 되었습니까 당신은 여전히해야합니까 어떤 문제? –
@DimitreNovatchev. 그것은 나에게 유용하다. 감사합니다. – Patan
222 : 환영합니다. –