這種轉變:
<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="#" 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>
產生想要的,正確的結果:
<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>
說明:
這是建立在你以前的問題的解決,有以下變化:
使用和身份規則的重寫。
而不是元素,創建具有作爲值分裂的結果的屬性。
要生成的屬性的名稱被指定爲全局my:names
元素的子項。
UPDATE:
在評論中OP已經修改了他的問題,他說:
我忘了添加名字空間 「一」 的標籤。所有「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:不客氣。 –