0

如何使用 @corresp 属性创建的显式交叉引用对 XML 源中元素 <seg> 的值进行分组?

<DIV>
<div id="fr">
  <seg id="fr_1" corresp="#en_1">abc</seg>
  <seg id="fr_2" corresp="#en_2 #en3">def</seg>
  <seg id="fr_3" corresp="#en_3 #en_4">ghi</seg>
  <seg id="fr_4" corresp="#en_4 #en_5">jkl</seg>
  <seg id="fr_5" corresp="#en_6">mno</seg>
</div>
<div id="en">
  <seg id="en_1" corresp="#fr_1">ab</seg>
  <seg id="en_2" corresp="#fr_1 #fr_2">cde</seg>
  <seg id="en_3" corresp="#fr_2 #fr_3">fg</seg>
  <seg id="en_4" corresp="#fr_3 #fr_4">hij</seg>
  <seg id="en_5" corresp="#fr_4">kl</seg>
  <seg id="en_6" corresp="#fr_5">mno</seg>
</div>
</DIV>

XSLT 转换后的输出应如下所示:

<div>
  <ul>
    <li>abc<br/>ab</li>
    <li>abc<br/>cde</li>
    <li>def<br/>cde</li>
    <li>def<br/>fg</li>
    <li>ghi<br/>fg</li>
    <li>ghi<br/>hij</li>
    <li>jkl<br/>hij</li>
    <li>jkl<br/>kl</li>
    <li>mno<br/>mno</li>
 </ul>
</div>

谢谢!

@Jirka,这是一个类似的输入,但在元素中嵌套了更多元素。而corresp 属性定义了这些新元素之间的交叉引用。修改后的输入如下所示:

<DIV>
<div id="fr">
  <seg id="fr_1"><w id="fr1" corresp="#en2">AA</w> <w id="fr2" corresp="#en3">BB</w>, <w id="fr3" corresp="#en1">CC</w>.</seg>
  <seg id="fr_2"><w id="fr1" corresp="#en1">DD</w> <w id="fr2" corresp="#en3">EE</w>?</seg>
  <seg id="fr_3"><w id="fr1" corresp="#en1">FF</w> <w id="fr2" corresp="#en3">GG</w><w id="fr3" corresp="#en2">HH</w></seg>
</div>
<div id="en">
<seg id="en_1"><w id="en1" corresp="#fr3">AX</w><w id="en2" corresp="#fr1">BX</w><w id="en3" corresp="#fr2">CX</w></seg>
 <seg id="en_2"><w id="en1" corresp="#fr1">DX</w><w id="en2" corresp="#en0">EX</w><w id="en3" corresp="#en2">FX</w></seg> 
 <seg id="en_3"><w id="en1" corresp="#fr1">HX</w> <w id="en2" corresp="#en3">IX</w><w id="en3" corresp="#en2">JX</w>.</seg>
</div>

所需的输出应如下所示:

<div>
 <ol>
  <li><w>AA</w> : <w>BX</w><br/><w>BB</w> : <w>AX</w><br/> </li>
  <li><w>DD</w> : <w>DX</w><w>EE</w> : <w>FX</w><br/><br/> </li>
  <li><w>FF</w> :<w>HX</w><br/> <w>GG</w> :<w>JX</w><br/><w>HH</w>:<w>IX</w><br/></li>
 </ol>
</div>
4

2 回答 2

0

您可以尝试对上一个问题的答案稍作调整:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:key name="kSegId" match="seg" use="@id"/>

    <xsl:template match="/DIV">
        <html>
            <head>
                <title></title>
            </head>
            <body>
                <ul>
                    <xsl:apply-templates select="div[@id='en']/seg"/>
                </ul>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="seg">
                <xsl:call-template name="findtarget">
                    <xsl:with-param name="targets" select="@corresp" />
                </xsl:call-template>

    </xsl:template>

    <xsl:template name="findtarget">
        <xsl:param name="targets" />
        <xsl:choose>
            <xsl:when test="contains($targets, ' ')">
                <xsl:call-template name="findtarget">
                    <xsl:with-param name="targets" select="substring-before($targets,' ')" />
                </xsl:call-template>
                <xsl:call-template name="findtarget">
                    <xsl:with-param name="targets" select="substring-after($targets,' ')" />
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <li>

                    <xsl:value-of select="key('kSegId',substring-after($targets, '#'))"/>
                    <br/>
                    <xsl:value-of select="."/>
                </li>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

这将生成以下输出:

<?xml version="1.0"?>
<html>
  <head>
    <title/>
  </head>
  <body>
    <ul>
      <li>abc<br/>ab</li>
      <li>abc<br/>cde</li>
      <li>def<br/>cde</li>
      <li>def<br/>fg</li>
      <li>ghi<br/>fg</li>
      <li>ghi<br/>hij</li>
      <li>jkl<br/>hij</li>
      <li>jkl<br/>kl</li>
      <li>mno<br/>mno</li>
    </ul>
  </body>
</html>
于 2013-07-11T14:01:14.130 回答
0

以下 xslt 可以工作

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

    <xsl:template match="/DIV">
        <div>
            <ul>
            <xsl:apply-templates select="div[@id = 'fr']/seg" />
            </ul>
        </div>
    </xsl:template>

    <xsl:template match="seg[../@id = 'fr']">
        <xsl:variable name="id" select="@id" />
        <xsl:variable name="text" select="." />

        <xsl:for-each select="//div[@id = 'en']/seg[contains(@corresp, $id)]">
            <li>
                <xsl:value-of select="$text" />
                <br />
                <xsl:value-of select="." />
            </li>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

这改变了我的输入

<?xml version="1.0" encoding="UTF-8"?>
<DIV>
    <div id="fr">
        <seg id="fr_1" corresp="#en_1">abc</seg>
        <seg id="fr_2" corresp="#en_2 #en3">def</seg>
        <seg id="fr_3" corresp="#en_3 #en_4">ghi</seg>
        <seg id="fr_4" corresp="#en_4 #en_5">jkl</seg>
        <seg id="fr_5" corresp="#en_6">mno</seg>
    </div>
    <div id="en">
        <seg id="en_1" corresp="#fr_1">ab</seg>
        <seg id="en_2" corresp="#fr_1 #fr_2">cde</seg>
        <seg id="en_3" corresp="#fr_2 #fr_3">fg</seg>
        <seg id="en_4" corresp="#fr_3 #fr_4">hij</seg>
        <seg id="en_5" corresp="#fr_4">kl</seg>
        <seg id="en_6" corresp="#fr_5">mno</seg>
    </div>
</DIV>

进入这个输出

<?xml version="1.0" encoding="UTF-8"?>
<div xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <ul>
        <li>abc<br/>ab</li>
        <li>abc<br/>cde</li>
        <li>def<br/>cde</li>
        <li>def<br/>fg</li>
        <li>ghi<br/>fg</li>
        <li>ghi<br/>hij</li>
        <li>jkl<br/>hij</li>
        <li>jkl<br/>kl</li>
        <li>mno<br/>mno</li>
    </ul>
</div>

如果您需要 html 输出,则必须更改 xsl:output 声明并在输入中添加一些 html 标记。

于 2013-07-11T14:04:04.613 回答