根据当前 XML 中的元素值,从另一个 XML 文件中获取元素 [英] Get Element from another XML file, based on an element value from current XML
问题描述
我使用以下 XSLT 代码从另一个 XML 文件中提取元素列表:
I use the following XSLT code to extract a list of elements from another XML file:
<xsl:variable name="titleCodes" select="document('other_XML.xml')/list"/>
other_XML.xml
文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="struktur_berufe.xsd">
<job id="15339" bkz="B 01104-101">
<type>t</type>
<name>
<name_nl>Job a</name_nl>
</name>
</job>
<job>
...
</job>
...
</list>
我现在想提取 job
元素的 id
属性,由 name_nl
元素过滤.我过滤的值来自我尝试转换的 XML 文件,如下所示:
I now want to extract the id
attribute of the job
element, filtered by the name_nl
element. The value by which I filter is from the XML file I am trying to transform, which looks like this:
<JobPost>
<id>1</id>
<title>
Job a <!-- value I filter by -->
</title>
</JobPost>
这是我目前尝试过的:
<TitleCode>
<xsl:value-of select="$titleCodes/job[type='t']/name[name_nl=title]/ancestor::job/@id"/>
</TitleCode>
然而,问题似乎是,[name_nl=title]
是空的,因为它在 other_XML.xml<中查找
title
元素/code>,它当然没有.
However, the problem seems to be, that [name_nl=title]
is empty, since it looks for thetitle
element in the other_XML.xml
, which it of course doesn't have.
我也尝试过使用这样的变量:
I also tried using a variable like this:
<TitleCode>
<xsl:variable select="title" name="ab"/>
<xsl:value-of select="$titleCodes/job[type='t']/name[name_nl=$ab]/ancestor::job/@id"/>
</TitleCode>
但仍然返回 ''
.
那么有没有办法解决这个问题?
So is there any way to solve this problem?
推荐答案
假设你在一个与 JobPost
匹配的模板中,你想要的表达式是这样的....
Assuming you are in a template matching JobPost
, the expression you want is this....
<xsl:value-of select="$titleCodes/job[type='t']/name[name_nl = current()/title]/ancestor::job/@id"/>
所以 current()/title
选择当前 JobPost
节点的 title
元素,而不是 name
表达式中的节点.
So current()/title
selects the title
element for the current JobPost
node, not the name
node in the expression.
如果这不起作用,可能是因为您的 XML 的 title
节点中有空格,所以试试这个...
If this doesn't work, it is probably because you have whitespace in the title
node in your XML, so try this...
<xsl:value-of select="$titleCodes/job[type='t']/name[name_nl = normalize-space(current()/title)]/ancestor::job/@id"/>
这可能是空格导致您第二次尝试使用该变量时出现问题.
It is probably the white space causing issues with your second attempt with the variable too.
注意你也可以这样写表达式,去掉表达式中ancestor
的使用
Note that you could also write the expression like this, to remove the use of ancestor
in the expression
<xsl:value-of select="$titleCodes/job[type='t'][name/name_nl = normalize-space(current()/title)]/@id" />
这篇关于根据当前 XML 中的元素值,从另一个 XML 文件中获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!