XML - XSLT - 使用来自外部 xml 文档的属性
XML - XSLT - Using attribute from external xml document
所以,我有这个 XML 输入文档:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<childs>
<child ID="10" name="John"/>
<child ID="2" name="Marie"/>
<child ID="7" name="Joseph"/>
<child ID="5" name="Daisy"/>
</childs>
<childInfo>
<info childID="10" name="John" age="15" gender="M"/>
<info childID="2" name="Marie" age="20" gender="F"/>
<info childID="7" name="Joseph" age="17" gender="M"/>
</childInfo>
</parent>
我还有第二个输入 XML 文档,我使用 document
函数访问它:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<name>Daisy</name>
<age>20</age>
<gender>F</gender>
</person>
因此,我想要做的是在第一个输入 XML 文档中添加另一个 <info>
元素,使用我从第二个(外部)XML 检索的数据] 文档,
这就是我要生成的输出 XML:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<childs>
<child ID="10" name="John"/>
<child ID="2" name="Marie"/>
<child ID="7" name="Joseph"/>
<child ID="5" name="Daisy"/>
</childs>
<childInfo>
<info childID="10" name="John" age="15" gender="M"/>
<info childID="2" name="Marie" age="20" gender="F"/>
<info childID="7" name="Joseph" age="17" gender="M"/>
<child childID="5" name="Daisy" age="20" gender="F"/>
</childInfo>
</parent>
这是我的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
expand-text="yes"
version="3.0">
<xsl:output indent="yes" />
<xsl:mode on-no-match="shallow-copy"/>
<xsl:variable name="externalDoc" select="document('externalStack.xml')"/>
<xsl:template match="parent/childInfo/info[last()]">
<xsl:variable name="nameOfChild" select="$externalDoc/person/name"/>
<xsl:variable name="idOfChild" select="parent/childs[@name = $nameOfChild]/@ID"/>
<xsl:next-match/>
<child>
<xsl:attribute name="childID">
<xsl:value-of select="$idOfChild" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="$externalDoc/person/name"/>
</xsl:attribute>
<xsl:attribute name="age">
<xsl:value-of select="$externalDoc/person/age"/>
</xsl:attribute>
<xsl:attribute name="gender">
<xsl:value-of select="$externalDoc/person/gender"/>
</xsl:attribute>
</child>
</xsl:template>
</xsl:stylesheet>
我可以访问所有数据字段。问题是 childID
属性。我想要做的是获取子项的 ID
属性的值,其中 name
属性等于第二个(外部)XML 中的元素 <name>
值文档。您可以看到我尝试使用两个 <xsl:variables>
来做到这一点,其中一个 (nameOfChild
) 获取外部文件中 <name>
元素的值,然后是第二个 (idOfChild
),它从 <child>
元素中的属性 ID
获取值,该属性的 name
值等于 nameOfChild
变量值。
有谁知道我该怎么做?因为使用两个变量似乎不起作用,
谢谢!
亚历山大·哈辛托
您正在这样做...
<xsl:variable name="idOfChild" select="parent/childs[@name = $nameOfChild]/@ID"/>
但是这是一个相对表达式,所以它是在当前info
节点下寻找一个parent
节点。您应该这样做以便检查整个文档(另请注意您错过了对 child
的引用)。
<xsl:variable name="idOfChild" select="/parent/childs/child[@name = $nameOfChild]/@ID"/>
或者,定义一个键
<xsl:key name="childs" match="child" use="@name" />
然后这样做...
<xsl:variable name="idOfChild" select="key('childs', $nameOfChild)/@ID"/>
所以,我有这个 XML 输入文档:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<childs>
<child ID="10" name="John"/>
<child ID="2" name="Marie"/>
<child ID="7" name="Joseph"/>
<child ID="5" name="Daisy"/>
</childs>
<childInfo>
<info childID="10" name="John" age="15" gender="M"/>
<info childID="2" name="Marie" age="20" gender="F"/>
<info childID="7" name="Joseph" age="17" gender="M"/>
</childInfo>
</parent>
我还有第二个输入 XML 文档,我使用 document
函数访问它:
<?xml version="1.0" encoding="UTF-8"?>
<person>
<name>Daisy</name>
<age>20</age>
<gender>F</gender>
</person>
因此,我想要做的是在第一个输入 XML 文档中添加另一个 <info>
元素,使用我从第二个(外部)XML 检索的数据] 文档,
这就是我要生成的输出 XML:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<childs>
<child ID="10" name="John"/>
<child ID="2" name="Marie"/>
<child ID="7" name="Joseph"/>
<child ID="5" name="Daisy"/>
</childs>
<childInfo>
<info childID="10" name="John" age="15" gender="M"/>
<info childID="2" name="Marie" age="20" gender="F"/>
<info childID="7" name="Joseph" age="17" gender="M"/>
<child childID="5" name="Daisy" age="20" gender="F"/>
</childInfo>
</parent>
这是我的 XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
expand-text="yes"
version="3.0">
<xsl:output indent="yes" />
<xsl:mode on-no-match="shallow-copy"/>
<xsl:variable name="externalDoc" select="document('externalStack.xml')"/>
<xsl:template match="parent/childInfo/info[last()]">
<xsl:variable name="nameOfChild" select="$externalDoc/person/name"/>
<xsl:variable name="idOfChild" select="parent/childs[@name = $nameOfChild]/@ID"/>
<xsl:next-match/>
<child>
<xsl:attribute name="childID">
<xsl:value-of select="$idOfChild" />
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="$externalDoc/person/name"/>
</xsl:attribute>
<xsl:attribute name="age">
<xsl:value-of select="$externalDoc/person/age"/>
</xsl:attribute>
<xsl:attribute name="gender">
<xsl:value-of select="$externalDoc/person/gender"/>
</xsl:attribute>
</child>
</xsl:template>
</xsl:stylesheet>
我可以访问所有数据字段。问题是 childID
属性。我想要做的是获取子项的 ID
属性的值,其中 name
属性等于第二个(外部)XML 中的元素 <name>
值文档。您可以看到我尝试使用两个 <xsl:variables>
来做到这一点,其中一个 (nameOfChild
) 获取外部文件中 <name>
元素的值,然后是第二个 (idOfChild
),它从 <child>
元素中的属性 ID
获取值,该属性的 name
值等于 nameOfChild
变量值。
有谁知道我该怎么做?因为使用两个变量似乎不起作用,
谢谢!
亚历山大·哈辛托
您正在这样做...
<xsl:variable name="idOfChild" select="parent/childs[@name = $nameOfChild]/@ID"/>
但是这是一个相对表达式,所以它是在当前info
节点下寻找一个parent
节点。您应该这样做以便检查整个文档(另请注意您错过了对 child
的引用)。
<xsl:variable name="idOfChild" select="/parent/childs/child[@name = $nameOfChild]/@ID"/>
或者,定义一个键
<xsl:key name="childs" match="child" use="@name" />
然后这样做...
<xsl:variable name="idOfChild" select="key('childs', $nameOfChild)/@ID"/>