我想将XML文档转换为HTML.某些XML元素具有指向其他文档的链接,例如:
<link href="1.html">
Run Code Online (Sandbox Code Playgroud)
在HTML输出中,我想得到:
<a href="1.html&no_cache={unique_id}">
Run Code Online (Sandbox Code Playgroud)
如何生成这个独特的大型ID?
首先,我假设由于某些未知原因,您无法link在所需的UID中使用绝对URL - 这是最简单和最自然的解决方案.
如果我的假设是正确的,那么:
对于XSLT来说,这是一项简单的任务.
因为OP在多次执行转换时希望生成的id相同,所以使用该generate-id()函数是不合适的.
以下是生成稳定ID的一种简单方法:
<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="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link[@href]">
<xsl:variable name="vUid">
<xsl:number level="any" count="link[@href]"/>
</xsl:variable>
<a href="{@href}&no_cache={{{$vUid}}}"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于以下XML文档时(无论多少次):
<t>
<link href="1.html"/>
<a>
<link href="2.html"/>
<b>
<link href="3.html"/>
<c>
<link href="4.html"/>
</c>
<link href="5.html"/>
</b>
<link href="6.html"/>
<d>
<link href="7.html"/>
</d>
</a>
<link href="8.html"/>
<e>
<link href="9.html"/>
</e>
<link href="10.html"/>
</t>
Run Code Online (Sandbox Code Playgroud)
每次产生想要的,相同的,正确的结果:
<t>
<a href="1.html&no_cache={1}"/>
<a>
<a href="2.html&no_cache={2}"/>
<b>
<a href="3.html&no_cache={3}"/>
<c>
<a href="4.html&no_cache={4}"/>
</c>
<a href="5.html&no_cache={5}"/>
</b>
<a href="6.html&no_cache={6}"/>
<d>
<a href="7.html&no_cache={7}"/>
</d>
</a>
<a href="8.html&no_cache={8}"/>
<e>
<a href="9.html&no_cache={9}"/>
</e>
<a href="10.html&no_cache={10}"/>
</t>
Run Code Online (Sandbox Code Playgroud)
请注意:使用<xsl:number>产生id.
如果在文档中可以多次出现相同的链接,并且我们需要所有出现使用相同的id,则以下是此问题的解决方案:
<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:key name="kHrefByVal" match="link/@href" use="."/>
<xsl:variable name="vUniqHrefs" select=
"//link/@href
[generate-id()
=
generate-id(key('kHrefByVal',.)[1])
]
"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="link[@href]">
<xsl:variable name="vthisHref" select="@href"/>
<xsl:variable name="vUid">
<xsl:for-each select="$vUniqHrefs">
<xsl:if test=". = $vthisHref">
<xsl:value-of select="position()"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<a href="{@href}&no_cache={{{$vUid}}}"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于以下XML文档时:
<t>
<link href="1.html"/>
<a>
<link href="2.html"/>
<b>
<link href="1.html"/>
<c>
<link href="3.html"/>
</c>
<link href="2.html"/>
</b>
<link href="1.html"/>
<d>
<link href="3.html"/>
</d>
</a>
<link href="4.html"/>
<e>
<link href="2.html"/>
</e>
<link href="4.html"/>
</t>
Run Code Online (Sandbox Code Playgroud)
产生了想要的正确结果:
<t>
<a href="1.html&no_cache={1}"/>
<a>
<a href="2.html&no_cache={2}"/>
<b>
<a href="1.html&no_cache={1}"/>
<c>
<a href="3.html&no_cache={3}"/>
</c>
<a href="2.html&no_cache={2}"/>
</b>
<a href="1.html&no_cache={1}"/>
<d>
<a href="3.html&no_cache={3}"/>
</d>
</a>
<a href="4.html&no_cache={4}"/>
<e>
<a href="2.html&no_cache={2}"/>
</e>
<a href="4.html&no_cache={4}"/>
</t>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10079 次 |
| 最近记录: |