问题描述
层次结构采用 XML 格式,我使用 XSLT 来转换数据.
The Hierarchy is in XML format and I am using XSLT to transform the data.
例如:如果层次结构有 4 个级别,而我想跳过中间的一个级别(比如级别 3):
For eg : If the Hierarchy has 4 levels and I want to skip a level in between (say level 3) :
Level 1
Level 2
Level 3 - Skip this level
Level 4
Level 5
我应该使用什么元素来实现相同的效果?
What element should I use to achieve the same?
附上我作为输入的示例 XML 文件以供参考
Attaching the sample XML file which I have as an input for reference
<?xml version="1.0" encoding="UTF-8"?>
<Hierarchy>
<Board>
<Name>President</Name>
<Id>ABCDE</Id>
<ParentId></ParentId>
<General>
<Name>President</Name>
<Description>Top level of the Hierarchy</Description>
<Template>LEVEL1</Template>
</General>
</Board>
<Board>
<Name>VP</Name>
<Id>EFGHI</Id>
<ParentId>ABCDE</ParentId>
<General>
<Name>VP</Name>
<Description>Below the President</Description>
<Template>LEVEL2</Template>
</General>
</Board>
<Board>
<Name>Department_Heads</Name>
<Id>JKLMN</Id>
<ParentId>EFGHI</ParentId>
<General>
<Name>Department_Heads</Name>
<Description>Reports to the VP</Description>
<Template>LEVEL3</Template>
</General>
</Board>
<Board>
<Name>Supervisors</Name>
<Id>OPQRS</Id>
<ParentId>JKLMN</ParentId>
<General>
<Name>Supervisors</Name>
<Description>Reports to the Reports to Dep Heads</Description>
<Template>LEVEL4</Template>
</General>
</Board>
<Board>
<Name>Employees</Name>
<Id>TUVWX</Id>
<ParentId>OPQRS</ParentId>
<General>
<Name>Department_Heads</Name>
<Description>Reports to the Reports to Dep Heads</Description>
<Template>LEVEL5</Template>
</General>
</Board>
</Hierarchy>
更新:我正在添加预期的输出和我目前使用的 xslt 转换.
Update : I am adding the expected output and the xslt transformation i currently use.
预期输出如下:
我目前使用的转换如下:
The transformation I am currently using is as below :
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Hierarchy>
<xsl:for-each select="Hierarchy/Board">
<xsl:if test="General/Template='LEVEL1'">
<xsl:variable name="Presidentchild" select="Id"/>
<Board>
<Name>
<xsl:value-of select="Name"/>
</Name>
<Template>PRESIDENT</Template>
<Description>
<xsl:value-of select = "General/Description"/>
</Description>
</Board>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="//Board">
<xsl:if test="ParentId = $Presidentchild">
<xsl:if test="General/Template='LEVEL2'">
<Board>
<xsl:variable name="VPchild" select="Id"/>
<Name>
<xsl:value-of select="Name"/>
</Name>
<Template>VICE_PRESIDENT</Template>
<Description>
<xsl:value-of select = "General/Description"/>
</Description>
</Board>
</xsl:if>
</xsl:if>
</xsl:for-each>
</Hierarchy>
</xsl:template>
</xsl:stylesheet>
........直到最后一个元素.
........so on till the last element.
预期的输出如下:
<?xml version="1.0" encoding="utf-8"?>
<Hierarchy>
<Board>
<Name>President</Name>
<Template>PRESIDENT</Template>
<Description>Top level of the Hierarchy</Description>
<Board>
<Name>VP</Name>
<Template>VICE_PRESIDENT</Template>
<Description>Below the President</Description>
</Board>
</Board>
</Hierachy>
... 直到最后一个元素.
... so on till the last element.
推荐答案
在对您的问题的评论中,我说这很琐碎.那是因为我认为您的输入 XML 具有层次结构.事实证明,需要通过跟踪从每个 Board
到其父级的交叉引用来派生层次结构.
In a comment to your question I said this is rather trivial. That is because I thought your input XML had a hierarchical structure. As it turns out, the hierarchy needs to be derived by tracing the cross-references from each Board
to its parent.
不过,因为每个 Board
还包含一个(错误命名的)Template
元素,用于指示其在层次结构中的级别,因此很容易跳过"某些级别,例如以下样式表中的 LEVEL3
:
Still, because each Board
also includes a (misnamed) Template
element that indicates its level in the hierarchy, it is easy to "skip" certain levels, such as LEVEL3
in the following stylesheet:
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:key name="children" match="Board" use="ParentId" />
<xsl:template match="/Hierarchy">
<xsl:copy>
<xsl:apply-templates select="Board[General/Template='LEVEL1']"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Board">
<xsl:copy>
<xsl:copy-of select="General/*"/>
<xsl:apply-templates select="key('children', Id)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Board[General/Template='LEVEL3']">
<xsl:apply-templates select="key('children', Id)"/>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例,这将产生:
Applied to your input example, this will produce:
结果
<?xml version="1.0" encoding="UTF-8"?>
<Hierarchy>
<Board>
<Name>President</Name>
<Description>Top level of the Hierarchy</Description>
<Template>LEVEL1</Template>
<Board>
<Name>VP</Name>
<Description>Below the President</Description>
<Template>LEVEL2</Template>
<Board>
<Name>Supervisors</Name>
<Description>Reports to the Reports to Dep Heads</Description>
<Template>LEVEL4</Template>
<Board>
<Name>Department_Heads</Name>
<Description>Reports to the Reports to Dep Heads</Description>
<Template>LEVEL5</Template>
</Board>
</Board>
</Board>
</Board>
</Hierarchy>
这篇关于是否可以使用 XSLT 跳过层次结构中的某个级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!