Xml 使用XSLT构建ID的嵌套层次结构
继续我的工作 XMLXml 使用XSLT构建ID的嵌套层次结构,xml,xslt,xpath,Xml,Xslt,Xpath,继续我的工作 XML XSL,这不起作用 此转换: <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:template match="
XSL,这不起作用
此转换:
<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:template match="/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="album">
<xsl:param name="pLastPath"/>
<xsl:variable name="vPath">
<xsl:for-each select="ancestor-or-self::album">
<xsl:value-of select="@title"/>
<xsl:if test="not(position()=last())">/</xsl:if>
</xsl:for-each>
</xsl:variable>
<div>
<xsl:if test="$pLastPath">
<xsl:attribute name="id"><xsl:value-of select="$pLastPath"/></xsl:attribute>
</xsl:if>
<a href="#{$vPath}"><xsl:value-of select="@title"/></a>
</div>
<xsl:apply-templates select="node()">
<xsl:with-param name="pLastPath" select="$vPath"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="image">
<xsl:param name="pLastPath"/>
<div id="{$pLastPath}">
<img id="{$pLastPath}/{@title}"/>
</div>
<xsl:apply-templates select="node()">
<xsl:with-param name="pLastPath" select="$pLastPath"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
<albums>
<album title="new_zealand">
<album title="auckland">
<image title="mt_eden_railroad_station"/>
</album>
</album>
</albums>
<div>
<a href="#new_zealand">new_zealand</a>
</div>
<div id="new_zealand">
<a href="#new_zealand/auckland">auckland</a>
</div>
<div id="new_zealand/auckland">
<img id="new_zealand/auckland/mt_eden_railroad_station"/>
</div>
/
应用于提供的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:template match="/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="album">
<xsl:param name="pLastPath"/>
<xsl:variable name="vPath">
<xsl:for-each select="ancestor-or-self::album">
<xsl:value-of select="@title"/>
<xsl:if test="not(position()=last())">/</xsl:if>
</xsl:for-each>
</xsl:variable>
<div>
<xsl:if test="$pLastPath">
<xsl:attribute name="id"><xsl:value-of select="$pLastPath"/></xsl:attribute>
</xsl:if>
<a href="#{$vPath}"><xsl:value-of select="@title"/></a>
</div>
<xsl:apply-templates select="node()">
<xsl:with-param name="pLastPath" select="$vPath"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="image">
<xsl:param name="pLastPath"/>
<div id="{$pLastPath}">
<img id="{$pLastPath}/{@title}"/>
</div>
<xsl:apply-templates select="node()">
<xsl:with-param name="pLastPath" select="$pLastPath"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
<albums>
<album title="new_zealand">
<album title="auckland">
<image title="mt_eden_railroad_station"/>
</album>
</album>
</albums>
<div>
<a href="#new_zealand">new_zealand</a>
</div>
<div id="new_zealand">
<a href="#new_zealand/auckland">auckland</a>
</div>
<div id="new_zealand/auckland">
<img id="new_zealand/auckland/mt_eden_railroad_station"/>
</div>
更新:@Tomalak担心斜杠在HTML id属性值中无效
请随意将此解决方案中的
“/”
字符替换为所需的任何有效字符(如“\u”
)。此转换:
<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:template match="/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="album">
<xsl:param name="pLastPath"/>
<xsl:variable name="vPath">
<xsl:for-each select="ancestor-or-self::album">
<xsl:value-of select="@title"/>
<xsl:if test="not(position()=last())">/</xsl:if>
</xsl:for-each>
</xsl:variable>
<div>
<xsl:if test="$pLastPath">
<xsl:attribute name="id"><xsl:value-of select="$pLastPath"/></xsl:attribute>
</xsl:if>
<a href="#{$vPath}"><xsl:value-of select="@title"/></a>
</div>
<xsl:apply-templates select="node()">
<xsl:with-param name="pLastPath" select="$vPath"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="image">
<xsl:param name="pLastPath"/>
<div id="{$pLastPath}">
<img id="{$pLastPath}/{@title}"/>
</div>
<xsl:apply-templates select="node()">
<xsl:with-param name="pLastPath" select="$pLastPath"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
<albums>
<album title="new_zealand">
<album title="auckland">
<image title="mt_eden_railroad_station"/>
</album>
</album>
</albums>
<div>
<a href="#new_zealand">new_zealand</a>
</div>
<div id="new_zealand">
<a href="#new_zealand/auckland">auckland</a>
</div>
<div id="new_zealand/auckland">
<img id="new_zealand/auckland/mt_eden_railroad_station"/>
</div>
/
应用于提供的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:template match="/*">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="album">
<xsl:param name="pLastPath"/>
<xsl:variable name="vPath">
<xsl:for-each select="ancestor-or-self::album">
<xsl:value-of select="@title"/>
<xsl:if test="not(position()=last())">/</xsl:if>
</xsl:for-each>
</xsl:variable>
<div>
<xsl:if test="$pLastPath">
<xsl:attribute name="id"><xsl:value-of select="$pLastPath"/></xsl:attribute>
</xsl:if>
<a href="#{$vPath}"><xsl:value-of select="@title"/></a>
</div>
<xsl:apply-templates select="node()">
<xsl:with-param name="pLastPath" select="$vPath"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="image">
<xsl:param name="pLastPath"/>
<div id="{$pLastPath}">
<img id="{$pLastPath}/{@title}"/>
</div>
<xsl:apply-templates select="node()">
<xsl:with-param name="pLastPath" select="$pLastPath"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
<albums>
<album title="new_zealand">
<album title="auckland">
<image title="mt_eden_railroad_station"/>
</album>
</album>
</albums>
<div>
<a href="#new_zealand">new_zealand</a>
</div>
<div id="new_zealand">
<a href="#new_zealand/auckland">auckland</a>
</div>
<div id="new_zealand/auckland">
<img id="new_zealand/auckland/mt_eden_railroad_station"/>
</div>
更新:@Tomalak担心斜杠在HTML id属性值中无效
请随意将此解决方案中的“/”
字符替换为所需的任何有效字符(如“\u”
)。此样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="albums">
<div>
<xsl:apply-templates mode="output"/>
</div>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="album">
<xsl:param name="pPath"/>
<xsl:variable name="vNextPath" select="concat($pPath,@title,'/')"/>
<div id="{$pPath}{@title}">
<xsl:apply-templates mode="output">
<xsl:with-param name="pPath" select="$vNextPath"/>
</xsl:apply-templates>
</div>
<xsl:apply-templates>
<xsl:with-param name="pPath" select="$vNextPath"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="image" mode="output">
<xsl:param name="pPath"/>
<img id="{$pPath}{@title}"/>
</xsl:template>
<xsl:template match="album" mode="output">
<xsl:param name="pPath"/>
<a href="#{$pPath}{@title}">
<xsl:value-of select="@title"/>
</a>
</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="albums">
<div>
<xsl:apply-templates mode="output"/>
</div>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="album">
<xsl:param name="pPath"/>
<xsl:variable name="vNextPath" select="concat($pPath,@title,'/')"/>
<div id="{$pPath}{@title}">
<xsl:apply-templates mode="output">
<xsl:with-param name="pPath" select="$vNextPath"/>
</xsl:apply-templates>
</div>
<xsl:apply-templates>
<xsl:with-param name="pPath" select="$vNextPath"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="image" mode="output">
<xsl:param name="pPath"/>
<img id="{$pPath}{@title}"/>
</xsl:template>
<xsl:template match="album" mode="output">
<xsl:param name="pPath"/>
<a href="#{$pPath}{@title}">
<xsl:value-of select="@title"/>
</a>
</xsl:template>
</xsl:stylesheet>
好问题(+1)。请参阅我的答案以获得完整、正确和简短的解决方案。好问题(+1)。请参阅我的答案以获得完整、正确和简短的解决方案。根据规范,斜杠在HTML ID中无效。@Tomalak:我不认为这是一个HTML问题,而是一个纯粹的XSLT问题。OP可以在字符串中使用他想要的任何内容,这不是此解决方案的本质。:)
/
在HTML5中是一个有效的id,更重要的是,它在所有浏览器中都能正常工作。目前在规范中找不到,但允许使用。根据规范,斜杠在HTML ID中无效。@Tomalak:我不认为这是一个HTML问题,而是一个纯粹的XSLT问题。OP可以在字符串中使用他想要的任何内容,这不是此解决方案的本质。:)/
在HTML5中是一个有效的id,更重要的是,它在所有浏览器中都能正常工作。现在在规范中找不到它,但允许它。