首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从子节点使用xslt从xml获取以下同级

如何从子节点使用xslt从xml获取以下同级
EN

Stack Overflow用户
提问于 2021-06-09 12:44:50
回答 3查看 101关注 0票数 0

我是XSL和XSLT的新手,我一直在尝试获取我的节点的以下同级节点,但在尝试了几种方法之后,它仍然不起作用。我正在使用这个glyph/image/following-siblings::image[1]/path来引用下面的兄弟。

这就是我的XML的结构。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<tablet id="A">
    <side id="Aa">
        <line id="Aa01">
            <glyph id="Aa01-001">
                <image id="Aa01-001-b" type="b">
                    <path id="path-1" d=""/>
                    <x>7.6107272</x>
                    <y>21.662689</y>
                    <width>52.389273</width>
                    <height>55.234216</height>
                </image>
                <image id="Aa01-001-f" type="f">
                    <path id="path-2" d=""/>
                    <x>9.9999</x>
                    <y>25.437278</y>
                    <width>34.33299</width>
                    <height>73.835859</height>
                </image>
            </glyph>
            <glyph id="Aa01-002">
                <image id="Aa01-002-b" type="b">
                    <path id="path-3" d=""/>
                    <x>58.25</x>
                    <y>21.41996</y>
                    <width>22.113771</width>
                    <height>75.019102</height>
                </image>
                <image id="Aa01-002-f" type="f">
                    <path id="path-4" d=""/>
                    <x>63.205795</x>
                    <y>24.00143</y>
                    <width>22.582803</width>
                    <height>74.659189</height>
                </image>
            </glyph>
    </line>
    </side>
</tablet>

我的XSLT如下所示。

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE xsl:stylesheet PUBLIC "Unofficial XSLT 1.0 DTD" "http://www.w3.org/1999/11/xslt10.dtd">

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 <xsl:output method="html" indent="yes"/>

 <!--  Main template  -->

 <xsl:template match="/">
  <div>
   <xsl:apply-templates select="tablet"/>
  </div>
 </xsl:template>

 <!-- Tablet -->

 <xsl:template match="tablet">
  <xsl:apply-templates select="side"/>
 </xsl:template> 

 <!-- Side -->

 <xsl:template match="side">
  <h2>
   <xsl:copy-of select="@id"/>
   <xsl:value-of select="side-name"/>
  </h2>
  <xsl:apply-templates select="line"/>
 </xsl:template>

 <!-- Line -->

 <xsl:template match="line">
  <xsl:variable name="curLine" select="line-num"/>
  <h3>
   <xsl:copy-of select="@id"/>
   <xsl:text>Line </xsl:text>
   <xsl:value-of select="line-num"/>
  </h3>
  <xsl:call-template name="drawGlyphLine"/>
 </xsl:template>

 <!-- Glyphs-->

 <xsl:template name="drawGlyphLine">
  <p>
   <xsl:text>Glyphs: </xsl:text>
   <xsl:value-of select="count(glyph/image[@type=$display-type])"/>
  </p>
  <div>
   <xsl:attribute name="height">
    <xsl:value-of select="160"/>
   </xsl:attribute>
   <xsl:attribute name="width">
    <xsl:value-of select="svg-width[@type=$display-type]"/>
   </xsl:attribute>
   <xsl:apply-templates select="glyph/image[@type=$display-type]"/>
  </div>
 </xsl:template>

 <!-- Glyph image -->

<xsl:template match="glyph/image">
 <svg>
     <g>
        <xsl:copy-of select="glyph/image/following-siblings::image[1]/path"> <!--Here I want previous sibling of path-->
        </xsl:copy-of>
     </g>
     <g>
     <xsl:copy-of select="path"> <!-- This line is making the output a copy of path-->
     </xsl:copy-of>
     </g>
     <g>
        <xsl:copy-of select="glyph/image/preceding-sibling::image[1]/path"> <!--Here I want next sibling of path-->
        </xsl:copy-of>
     </g>
  </svg>
 </xsl:template>

</xsl:stylesheet>

在输出中,我想要当前路径的以下同级。我没有添加完整的代码,因为它不会让它变得不必要的长。基本上,完整的代码会自动复制路径并呈现SVG。现在我只想要下一个和前一个同级。

现在输出应该是这样的。

代码语言:javascript
复制
<svg>
  <!-- previous path node -->

  <path id="path-1" d=""/>
  <x>7.6107272</x>
  <y>21.662689</y>
  <width>52.389273</width>
  <height>55.234216</height>

  <!-- next path node-->
</svg>
<svg>
  <!-- previous path node -->
  <path id="path-2" d=""/>
  <x>7.6107272</x>
  <y>21.662689</y>
  <width>52.389273</width>
  <height>55.234216</height>
  <!-- next path node -->
</svg>
<svg>
  <!-- previous path node -->
  <path id="path-3" d=""/>
  <x>7.6107272</x>
  <y>21.662689</y>
  <width>52.389273</width>
  <height>55.234216</height>
  <!-- next path node -->
</svg>
<svg>
  <!-- previous path node -->
  <path id="path-4" d=""/>
  <x>7.6107272</x>
  <y>21.662689</y>
  <width>52.389273</width>
  <height>55.234216</height>
  <!-- next path node -->
</svg>
<svgg>
  <!-- previous path node -->
  <path id="path-5" d=""/>
  <x>7.6107272</x>
  <y>21.662689</y>
  <width>52.389273</width>
  <height>55.234216</height>
  <!-- next path node -->
<svg>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-06-10 15:00:24

您的示例具有唯一的图像ID。但是,如果要向前和向后遍历,可以使用preceding轴。也就是说,如果它不会导致任何性能问题。

glyph/image模板中,首先将@id存储在变量中

代码语言:javascript
复制
<xsl:variable name="curr_id" select="@id"/>

然后在前面或后面的轴中遍历时使用它

代码语言:javascript
复制
 preceding::image[@id=$curr_id][1]/path
 following::image[@id=$curr_id][1]/path

您的模板应如下所示:

代码语言:javascript
复制
<xsl:template match="glyph/image">
    <xsl:variable name="curr_id" select="@id"/>
    <svg>
        <g>
            <xsl:copy-of select="preceding::image[@id=$curr_id][1]/path"> <!--Here I want previous sibling of path-->
            </xsl:copy-of>
        </g>
        <g>
            <xsl:copy-of select="path"> <!-- This line is making the output a copy of path-->
            </xsl:copy-of>
        </g>
        <g>
            <xsl:copy-of select="following::image[@id=$curr_id][1]/path"> <!--Here I want next sibling of path-->
            </xsl:copy-of>
        </g>
    </svg>
</xsl:template>

我已经在下面的链接(http://xsltransform.net/pPgCH6G)中修改了您的示例。如果我的假设是正确的,这应该是可行的。

票数 1
EN

Stack Overflow用户

发布于 2021-06-09 15:42:35

在此模板中

代码语言:javascript
复制
<xsl:template match="glyph/image">
 <svg>
     <g>
        <xsl:copy-of select="glyph/image/following-siblings::image[1]/path"> <!--Here I want previous sibling of path-->
        </xsl:copy-of>
     </g>
     <g>

上下文项是一个图像元素,因此字形/图像的选择是错误的。你只想要<xsl:copy-of select="following-siblings::image[1]/path">

票数 1
EN

Stack Overflow用户

发布于 2021-06-09 18:16:27

你的意思是:

代码语言:javascript
复制
  <xsl:template match="image">
    <svg>
      <xsl:copy-of select="preceding::image[1]/path"/>
      <xsl:copy-of select="*"/>
      <xsl:copy-of select="following::image[1]/path"/>
    </svg>
  </xsl:template>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67897747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档