您可以使用客户端 XSLT。在您的 XML 文档中提供 PI,并在特定样式表中包含主布局样式表。
请随意查看和使用http://www.aranedabienesraices.com.ar作为示例。
EDIT 3:几乎完整的递归示例。
XML 文档“layoutA.xml”:
<html xmlns:inc="include">
<body>
<h1>Birthday</h1>
<dl inc:in-iter="person">
<dt inc:path="name"></dt>
<dd inc:path="date"></dd>
</dl>
</body>
</html>
输入 XML 文档:
<data>
<person>
<name>Bob</name>
<date>2010-02-23</date>
<link>http://example.org/bob</link>
</person>
<person>
<name>Alex</name>
<date>2010-02-23</date>
<link>http://example.org/alex</link>
</person>
</data>
样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:inc="include">
<xsl:param name="pLayout" select="'layoutA.xml'"/>
<xsl:template match="/">
<xsl:apply-templates select="document($pLayout)/*">
<xsl:with-param name="context" select="*"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:param name="context"/>
<xsl:copy>
<xsl:apply-templates select="@*|node()">
<xsl:with-param name="context" select="$context"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@inc:path]">
<xsl:param name="context"/>
<xsl:copy>
<xsl:apply-templates select="@*">
<xsl:with-param name="context" select="$context"/>
</xsl:apply-templates>
<xsl:value-of select="$context/*[name()=current()/@inc:path]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@inc:in-iter]" priority="1">
<xsl:param name="context"/>
<xsl:variable name="me" select="."/>
<xsl:copy>
<xsl:apply-templates select="@*">
<xsl:with-param name="context" select="$context"/>
</xsl:apply-templates>
<xsl:for-each select="$context/*[name()=current()/@inc:in-iter]">
<xsl:apply-templates select="$me/node()">
<xsl:with-param name="context" select="."/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="*[@inc:out-iter]" priority="1">
<xsl:param name="context"/>
<xsl:variable name="me" select="."/>
<xsl:for-each select="$context/*[name()=current()/@inc:out-iter]">
<xsl:element name="{name($me)}" namespace="{namespace-uri($me)}">
<xsl:apply-templates select="$me/@*|$me/node()">
<xsl:with-param name="context" select="."/>
</xsl:apply-templates>
</xsl:element>
</xsl:for-each>
</xsl:template>
<xsl:template match="@inc:path|@inc:in-iter|@inc:out-iter" priority="1"/>
<xsl:template match="@inc:*">
<xsl:param name="context"/>
<xsl:attribute name="{local-name()}">
<xsl:value-of select="$context/*[name()=current()]"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
输出:
<html xmlns:inc="include">
<body>
<h1>Birthday</h1>
<dl>
<dt>Bob</dt>
<dd>2010-02-23</dd>
<dt>Alex</dt>
<dd>2010-02-23</dd>
</dl>
</body>
</html>
将参数pLayout 传递为'layoutB.xml',并且这个“layoutB.xml”:
<html xmlns:inc="include">
<body>
<h1>Friends</h1>
<ul>
<li inc:out-iter="person">
<a inc:href="link" inc:path="name"></a>
</li>
</ul>
</body>
</html>
输出:
<html xmlns:inc="include">
<body>
<h1>Friends</h1>
<ul>
<li>
<a href="http://example.org/bob">Bob</a>
</li>
<li>
<a href="http://example.org/alex">Alex</a>
</li>
</ul>
</body>
</html>
注意:您的要求的主要问题是相同的文档限制(因此,相同的文档 URI,没有不同的 PI,没有不同的布局 URI 元数据),这让您只需要 javascript 来传递布局URI 参数。直到浏览器支持 XPath 2.0 fn:document-uri() 才能解析 URL 查询。当然,您可以使用一些扩展(例如 MSXSL script),但很难跨浏览器运行。