XSLT - 在 for-each 循环中寻找变量元素
XSLT - Looking for variable elements in for-each loops
我有一个 XML 文档,我正在将其转换为多个 HTML 文档。问题是对于生成的每个文档,我需要在单独的 XML 文件集合中查找不同的节点。
想象一下我的 XML 看起来像这样:
<index>
<item>
<species>Dog</species>
<tagName>canine</tagName>
</item>
<item>
<species>Cat</species>
<tagName>feline</tagName>
</item>
<index>
我收集了数十个文件,这些文件中散布着名为 'canine' 和 'feline' 的元素。我需要为每份文件调用正确的文件。
我的 XSLT 如下所示:
<xsl:template match="/">
<xsl:for-each select="index/item">
<xsl:result-document method="xml" href="{species}.html">
<xsl:for-each select="collection('index.xml')//canine">
<xsl:value-of select=".">
</xsl:for-each>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
我正在寻找一种将“//canine”转换为变量的方法,以便在 Dog 文档中查找 ,在 Cat 文档中查找 等等。
我不知道该怎么做。谁能指出我正确的方向?我一直在搞乱变量,但我找不到任何有用的东西。
I'm looking for a way to turn that "//canine" into a variable so that
in the Dog document it looks for <canine>
, in the Cat document it
looks for <feline>
etc etc.
试试这样的东西:
<xsl:for-each select="collection('index.xml')//*[name()=current()/tagName]">
注意:
- 最好使用 key 来按名称 select 元素 -
但显然当你的目标是 collection 时你不能这样做
(?);
- 我不确定您的 collection 定义是否正确。但这将是另一个问题的主题。
根据我们未显示的代码,您可能会也可能不会使用 XSLT current()
函数。
此代码不使用 current()
:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="index/item">
<xsl:result-document method="xml" href="{species}.html">
<xsl:sequence select=
"for $tag in tagName[1]
return collection('index.xml')//*[name() eq $tag]/string()"/>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
如果你想使用key()
函数(可能加速转换),试试这个:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kElemByName" match="*" use="name()"/>
<xsl:template match="index/item">
<xsl:result-document method="xml" href="{species}.html">
<xsl:sequence select=
"for $t in tagName[1], $doc in collection('index.xml')
return key('kElemByName', $t, $doc)"/>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
我有一个 XML 文档,我正在将其转换为多个 HTML 文档。问题是对于生成的每个文档,我需要在单独的 XML 文件集合中查找不同的节点。
想象一下我的 XML 看起来像这样:
<index>
<item>
<species>Dog</species>
<tagName>canine</tagName>
</item>
<item>
<species>Cat</species>
<tagName>feline</tagName>
</item>
<index>
我收集了数十个文件,这些文件中散布着名为 'canine' 和 'feline' 的元素。我需要为每份文件调用正确的文件。
我的 XSLT 如下所示:
<xsl:template match="/">
<xsl:for-each select="index/item">
<xsl:result-document method="xml" href="{species}.html">
<xsl:for-each select="collection('index.xml')//canine">
<xsl:value-of select=".">
</xsl:for-each>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
我正在寻找一种将“//canine”转换为变量的方法,以便在 Dog 文档中查找
我不知道该怎么做。谁能指出我正确的方向?我一直在搞乱变量,但我找不到任何有用的东西。
I'm looking for a way to turn that "//canine" into a variable so that in the Dog document it looks for
<canine>
, in the Cat document it looks for<feline>
etc etc.
试试这样的东西:
<xsl:for-each select="collection('index.xml')//*[name()=current()/tagName]">
注意:
- 最好使用 key 来按名称 select 元素 - 但显然当你的目标是 collection 时你不能这样做 (?);
- 我不确定您的 collection 定义是否正确。但这将是另一个问题的主题。
根据我们未显示的代码,您可能会也可能不会使用 XSLT current()
函数。
此代码不使用 current()
:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="index/item">
<xsl:result-document method="xml" href="{species}.html">
<xsl:sequence select=
"for $tag in tagName[1]
return collection('index.xml')//*[name() eq $tag]/string()"/>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
如果你想使用key()
函数(可能加速转换),试试这个:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kElemByName" match="*" use="name()"/>
<xsl:template match="index/item">
<xsl:result-document method="xml" href="{species}.html">
<xsl:sequence select=
"for $t in tagName[1], $doc in collection('index.xml')
return key('kElemByName', $t, $doc)"/>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>