XSLT:在XSLT中创建和存储地图

时间:2016-09-05 08:29:44

标签: xml xslt xpath

我是xslt的新手,所以请放轻松我。我有这样的xml。

  <?xml version="1.0" encoding="UTF-8"?>
  <?xml-stylesheet type="text/xsl" version="1.0"?>
  <process>
     <groups>
        <group id="sid-17d5a8eaba5e4313bd4958e74f25d3df" name="GroupA">
           <person-ref>sid-446515B9-2247-4657-A612-4693825B1ACF</person-ref>
           <person-ref>sid-A57CAAA3-5FBA-4E8B-B530-A69571FDDF9A</person-ref>
           <person-ref>sid-CFEC4F6D-2614-4887-90DE-9FE13AE44091</person-ref>
        </group>
        <group id="sid-d9dc88f7077046e9825e87c349d75909" name="GroupB">
           <person-ref>sid-BE136A0A-3A44-4C4A-B661-6F606D64AA94</person-ref>
           <person-ref>sid-E9C5B00D-BF64-4560-96E4-EE111CC98AB4</person-ref>
           <person-ref>sid-B2217776-D570-43A7-8110-A11026389EE5</person-ref>
        </group>
     </groups>

     <persons>
        <person id="sid-446515B9-2247-4657-A612-4693825B1ACF"/>
        <person id="sid-B2217776-D570-43A7-8110-A11026389EE5"/>
     </persons>
  </process>

我喜欢实现的是获得每个人的相应群体,如下所示:

  <persons>
     <person id="sid-446515B9-2247-4657-A612-4693825B1ACF">
        <Link name="GroupA" targetId="sid-17d5a8eaba5e4313bd4958e74f25d3df" />
     </person>
     <person id="sid-B2217776-D570-43A7-8110-A11026389EE5">
        <Link name="GroupB" targetId="sid-d9dc88f7077046e9825e87c349d75909" />
     </person>
  </persons>

我的方法是创建组的地图,然后将其全局存储。我已经通过互联网上的一些研究做了但是xslt似乎让我们很难创建地图。

2 个答案:

答案 0 :(得分:1)

  

我的方法是创建组的地图然后存储它   全局。

我不确定你的意思&#34; 创建一个群组地图,然后将其全局存储。&#34;这里首选的方法是使用key查找来自相应组的数据:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="my-group" match="group" use="person-ref" />

<xsl:template match="/process">
    <persons>
        <xsl:for-each select="persons/person">
            <xsl:variable name="group" select="key('my-group', @id)" />
            <person id="{id}">
                <Link name="{$group/@name}" targetId="{$group/@id}" />
            </person>
        </xsl:for-each>
    </persons>
</xsl:template>

</xsl:stylesheet>

答案 1 :(得分:-1)

试试这个

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" />
    <xsl:key name="my-group" match="group" use="person-ref" />
    <xsl:template match="/process/groups">
        <persons>
            <xsl:for-each select = "group/person-ref">
                <xsl:variable name="ref" select="." />
                <xsl:variable name="group" select="key('my-group', $ref)" />
                <person id="{$ref}">
                    <Link name="{$group/@name}" targetId="{$group/@id}" />
                </person>
            </xsl:for-each>
        </persons>
    </xsl:template>
</xsl:stylesheet>