我正在尝试使用xsl:key来使用XSL document()函数查找外部XML文档中的项目.我可以让xsl:key部分工作,如果,而不是使用document(),我只是合并两个XML文件(在C#中使用XmlDocument).然而,两个XML文件都非常大,并且在某些情况下我开始出现"内存不足"错误.此外,我需要能够使用xls:key,否则该过程需要数小时.
在XSLT 2.0中,我相信你可以这样做:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="lookupDoc" select="document('CodeDescriptions.xml')" />
<xsl:key name="LookupDescriptionByCode" match="Code/@description" use="../@code" />
<xsl:template match="ItemCode">
<xsl:call-template name="MakeSpanForCode">
<xsl:with-param name="code" select="text()" />
</xsl:call-template>
</xsl:template>
<xsl:template name="MakeSpanForCode">
<xsl:param name="code" />
<xsl:element name="span">
<xsl:attribute name="title">
<xsl:value-of select="$lookupDoc/key('LookupDescriptionByCode', $code)" />
</xsl:attribute>
<xsl:value-of select="$code" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
你如何在XSLT 1.0中实现这一目标?
我发现xsl:key似乎无法正常工作的情况。
我正在将XSLT 1与Xalan(已编译)一起使用,这就是发生的情况:
1.-这有效:名为test1的键可以正常工作:
<xsl:variable name="promosRTF">
<xsl:copy-of select="promo[@valid='true']"/>
</xsl:variable>
<xsl:variable name="promos" select="xalan:nodeset($promosRTF)" />
<!-- now the key: when defined and used here it works fine: -->
<xsl:key name="test1" match="//promo" use="'anytext'" />
<xsl:for-each select="key('test1','anytext')/*">
loop through elements in key... ok, works fine
</xsl:for-each>
<xsl:for-each select="$promos/*">
..loop through elements in variable $promos ...it is not empty so the loop iterates several times
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
2.-这不起作用:现在在循环遍历xalan:nodeset获得的元素的循环内定义并使用了密钥test1(我想这是重点)。
<xsl:variable name="promosRTF">
<xsl:copy-of select="promo[@valid='true']"/>
</xsl:variable>
<xsl:variable name="promos" select="xalan:nodeset($promosRTF)" />
<xsl:for-each …Run Code Online (Sandbox Code Playgroud) 说我有以下XML ...
<root>
<base>
<tent key="1" color="red"/>
<tent key="2" color="yellow"/>
<tent key="3" color="blue"/>
</base>
<bucket>
<tent key="1"/>
<tent key="3"/>
</bucket>
</root>
Run Code Online (Sandbox Code Playgroud)
... XPath会返回"bucket"包含"red"和"blue"的内容?
我知道如果我有这样的XML文件:
<persons>
<class name="English">
<person name="Tarzan" id="050676"/>
<person name="Donald" id="070754"/>
<person name="Dolly" id="231256"/>
</class>
<class name="Math">
<person name="Winston" id="050677"/>
<person name="Donald" id="070754"/>
<person name="Fred" id="231257"/>
</class>
</persons>
Run Code Online (Sandbox Code Playgroud)
我可以像这样在XSL文件中定义一个键:
<xsl:key name="preg" match="person" use="@id"/>
Run Code Online (Sandbox Code Playgroud)
我在哪里使用id作为密钥.然而,唐纳德被列入两次,但只是在preg的一个地方.
假设我希望他在preg中列出两次.也就是说,我想让类名成为标识符的一部分.基本上,我希望preg具有等价于有序对的键:( class-name,id).我该怎么做(使用XSLT 1.0)?
我正在尝试使用XSLT key()函数返回<Code>XML文件中符合以下两个条件的所有元素:
Code[code=$code] AND ancestor::CodeType[type=$codeType]`
Run Code Online (Sandbox Code Playgroud)
以下是输入XML的简单示例:
<Items>
<Item code-number="C1" category="ABC" />
<Item code-number="C3" category="ABC" />
<Item code-number="C1" category="XYZ" />
</Items>
<CodeTypes>
<CodeType type="ABC">
<SubType title="Category III Codes"> <!-- <SubType> elements are optional -->
<SubType title="Subcategory III-15 Codes">
<Code code="C1" description="Red" />
<Code code="C2" description="Green" />
<Code code="C3" description="Blue" />
<Code code="C3" description="Purple" /> <!-- Same code can appear more than once -->
</SubType>
</SubType>
<CodeType>
<CodeType type="XYZ">
<Code code="C1" description="Black" /> <!-- Same code can be used for …Run Code Online (Sandbox Code Playgroud) 我四处搜索并找到了一些关于 xsl:key 和 key() 函数的教程,但不知何故,我显然仍然缺少一些理解。
我需要进行 XML-XML 转换,其中包括大约 10 个字段,您必须在这些字段中从源 XML 获取字符串值,从适当的查找表(提供)中找到适当的数字代码,并将这些代码放入生成的 XML 中。
我有一个这样做的工作版本 xsl:for-each for lookup table,但我怀疑它是次优的,想知道我是否应该使用 select="key('CR-Lookup',$CR)" 而不是 somhow .
所以,我想做的是(树的深处):
<Contributor>
<ContributorRole>producer</ContributorRole>
<ContributorName>Anglet, J.</ContributorName>
</Contributor>
Run Code Online (Sandbox Code Playgroud)
变成这样的东西:
<Contributor>
<ContributorRole id="7" code="818"/>
<Value id="Name">Anglet, J.</Value>
</Contributor>
Run Code Online (Sandbox Code Playgroud)
我制作的文件如下:
查找表文件lookup_ContributorRole.xml:
<lookup id="ContributorRole">
<row>
<id>7</id>
<parentid>NULL</parentid>
<valueMember>1</valueMember>
<displayMember>producer</displayMember>
<code>818</code>
<externalId>NULL</externalId>
<description>NULL</description>
</row>
<!-- more <row>s...-->
</lookup>
Run Code Online (Sandbox Code Playgroud)
Amd XSLT 文件,我尝试在其中进行匹配:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foxml="info:fedora/fedora-system:def/foxml#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rel="info:fedora/fedora-system:def/relations-external#"
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
xmlns:audit="info:fedora/fedora-system:def/audit#"
xmlns:fedoraxsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="xsl foxml rdf rel oai_dc dc xsi audit …Run Code Online (Sandbox Code Playgroud)