XSL 交叉引用 [英] XSL cross-reference

查看:21
本文介绍了XSL 交叉引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习 XSL,有一个关于交叉引用的问题.我的目标 XML 文件的结构如下:

I'm learning XSL right now and have a question regarding cross-references. My target XML file is structured like this:

<XML>
    <Model>
        <packedElement typ="class" id="1"/>
        <packedElement typ="class" id="2"/>
        <packedElement typ="class" id="3"/>
    </Model>
    <Elements>
        <Element idref="1">
            <Attributes comment="comment 1."/>
        </Element>
        <Element idref="2">
            <Attributes comment="comment 2."/>
        </Element>
        <Element idref="3">
            <Attributes comment="comment 3."/>
        </Element>
    </Elements>
</XML>

我想连接 id=idref.我的目标是列出所有packedElements 并打印他们的评论.你们能帮我吗?

I want to connect id=idref. My goal is to list all packedElements and print their comment. Can u guys help me?

我试图用 key-funktion 解决它,但我不是很成功.

I tried to resolve it with the key-funktion, but I wasn't very succesful.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" encoding="UTF-8"/>

<xsl:key name="CommentK" match="Element" use="@idref"/>


<xsl:template match="XML">
<XML>
<xsl:apply-templates/>
</XML>
</xsl:template>

<xsl:template name="Start" match="packedElement">
<xsl:variable name="TEST" select="@id"/>
<xsl:variable name="Comment">
<xsl:call-template name="FindComment">
<xsl:with-param name="test2" select="@id"/>
</xsl:call-template>
</xsl:variable>
<content comment="{$Comment}" id ="{@id}" test="{$TEST}"></content>
</xsl:template>

<xsl:template name="FindComment">
<xsl:param name="test2"/>

<xsl:for-each select="key('CommentK', '$test2')">

<xsl:value-of select="Attributes/@comment"/>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

XSLT 版本是 2.0.(顺便说一句,有人能告诉我 .XSLT 和 .XSL 之间的区别吗?)

XSLT version is 2.0. (Btw can someone tell me the difference between the .XSLT and .XSL?)

推荐答案

尝试

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="el-by-idref" match="Elements/Element" use="@idref"/>

<xsl:template match="XML">
  <xsl:copy>
    <xsl:apply-templates select="Model/packedElement"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Model/packedElement">
  <content comment="{key('el-by-idref', @id)/Attributes/@comment}" id="{@id}" test="{@id}"/>
</xsl:template>



</xsl:stylesheet>

这样就可以了

<XML>
   <content comment="comment 1." id="1" test="1"/>
   <content comment="comment 2." id="2" test="2"/>
   <content comment="comment 3." id="3" test="3"/>
</XML>

这篇关于XSL 交叉引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆