XSLT 外部查找表正确用法 - key() 函数?

发布于 2024-07-19 08:09:43 字数 3461 浏览 2 评论 0原文

我四处搜索并找到了一些有关 xsl:key 和 key() 函数的教程,但不知怎的,我显然仍然缺少一些理解。

我需要执行 XML-XML 转换,其中包括大约 10 个字段,您必须从源 XML 中获取字符串值,从适当的查找表(提供)中找到适当的数字代码,并将这些代码放入结果 XML 中。

我有一个执行 xsl:for-each 查找表的工作版本,但我怀疑它不是最佳的,想知道我是否应该使用 select="key('CR-Lookup',$CR)" 代替 somhow 。

所以,我想做的是(树的深处):

<Contributor>
<ContributorRole>producer</ContributorRole>
<ContributorName>Anglet, J.</ContributorName>
</Contributor>

转换成这样的东西:

<Contributor>
<ContributorRole id="7" code="818"/>
<Value id="Name">Anglet, J.</Value>
</Contributor>

我制作的文件如下:

查找表文件 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>

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 fedoraxsi"
>
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />

<xsl:key name="CR-lookup" match="row" use="displayMember"/>
<xsl:variable name="CRTable" select="document('lookup_ContributorRole.xml')/lookup/row"/>

<xsl:template match="Contributor">
    <Contributor>
    <xsl:variable name="CR"><xsl:value-of select="ContributorRole"/></xsl:variable>
    <ContributorRole>
    <xsl:for-each select="$CRTable">
        <xsl:if test="displayMember=$CR">
            <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute>
            <xsl:attribute name="code"><xsl:value-of select="code"/></xsl:attribute>
        </xsl:if>
    </xsl:for-each>
    </ContributorRole>
    <Value id="Name"><xsl:value-of select="ContributorName"/></Value>
    </Contributor>
</xsl:template>

    <xsl:template match="/">
    <DigitalObject>
        <Core>
            <xsl:for-each select="/foxml:digitalObject/foxml:datastream[@ID='DigitalObjectLL']/foxml:datastreamVersion">
             <xsl:sort select="@CREATED" order="descending"/>
             <xsl:if test="position() = 1">
                <xsl:for-each select="./foxml:xmlContent/lnbdo">
                    <xsl:apply-templates select="Contributor"/>
                </xsl:for-each>
             </xsl:if>
           </xsl:for-each>
        </Core>
    </DigitalObject>
    </xsl:template>
</xsl:stylesheet>

I have searched around and found some tutorials for xsl:key and key() function, but somehow I still am missing some understanding apparently.

There is a XML-XML transformation I need to do, which includes some 10 fields where you I have to take string values from source XML, find appropriate numeric codes from appropriate lookup tables (provided), and put those codes in resulting XML.

I have a working version of this doing xsl:for-each for lookup table, but I suspect it is suboptimal and would like to know if I should have used select="key('CR-Lookup',$CR)" instead somhow.

So, what I want to do is (deep part of tree):

<Contributor>
<ContributorRole>producer</ContributorRole>
<ContributorName>Anglet, J.</ContributorName>
</Contributor>

to be transformed into something like this:

<Contributor>
<ContributorRole id="7" code="818"/>
<Value id="Name">Anglet, J.</Value>
</Contributor>

The files I've made like thus:

Lookup table file 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>

Amd the XSLT file, where I attempt to do the matching:

<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 fedoraxsi"
>
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml" />

<xsl:key name="CR-lookup" match="row" use="displayMember"/>
<xsl:variable name="CRTable" select="document('lookup_ContributorRole.xml')/lookup/row"/>

<xsl:template match="Contributor">
    <Contributor>
    <xsl:variable name="CR"><xsl:value-of select="ContributorRole"/></xsl:variable>
    <ContributorRole>
    <xsl:for-each select="$CRTable">
        <xsl:if test="displayMember=$CR">
            <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute>
            <xsl:attribute name="code"><xsl:value-of select="code"/></xsl:attribute>
        </xsl:if>
    </xsl:for-each>
    </ContributorRole>
    <Value id="Name"><xsl:value-of select="ContributorName"/></Value>
    </Contributor>
</xsl:template>

    <xsl:template match="/">
    <DigitalObject>
        <Core>
            <xsl:for-each select="/foxml:digitalObject/foxml:datastream[@ID='DigitalObjectLL']/foxml:datastreamVersion">
             <xsl:sort select="@CREATED" order="descending"/>
             <xsl:if test="position() = 1">
                <xsl:for-each select="./foxml:xmlContent/lnbdo">
                    <xsl:apply-templates select="Contributor"/>
                </xsl:for-each>
             </xsl:if>
           </xsl:for-each>
        </Core>
    </DigitalObject>
    </xsl:template>
</xsl:stylesheet>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∝单色的世界 2024-07-26 08:09:43

您必须先切换上下文文档,然后才能使用密钥:

<xsl:variable name="CRTable" select="document('lookup_ContributorRole.xml')"/>

<xsl:template match="Contributor">
  <Contributor>
    <xsl:variable name="CR" select="ContributorRole"/>
    <ContributorRole>
      <xsl:for-each select="$CRTable"><!-- change context document -->
        <xsl:for-each select="key('CR-lookup', $CR)">
          <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute>
          <xsl:attribute name="code"><xsl:value-of select="code"/></xsl:attribute>
          ...

使用 XSLT 2.0,您可以执行以下操作:

<xsl:for-each select="key('CR-lookup', $CR, $CRTable)">

You have switch the context document before you can use the key:

<xsl:variable name="CRTable" select="document('lookup_ContributorRole.xml')"/>

<xsl:template match="Contributor">
  <Contributor>
    <xsl:variable name="CR" select="ContributorRole"/>
    <ContributorRole>
      <xsl:for-each select="$CRTable"><!-- change context document -->
        <xsl:for-each select="key('CR-lookup', $CR)">
          <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute>
          <xsl:attribute name="code"><xsl:value-of select="code"/></xsl:attribute>
          ...

With XSLT 2.0 you could do

<xsl:for-each select="key('CR-lookup', $CR, $CRTable)">
逆夏时光 2024-07-26 08:09:43

我认为您需要知道使用 Xslt 的最佳方法关键功能

I think you need to know the best way of using the Xslt key function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文