转换节点中的每个子节点

时间:2014-08-21 12:16:05

标签: xml xslt

我认为我太愚蠢或者其他......但是......如果存在,如何转换父节点中的子节点?

我有一个xml文件,如下所示......

    <?xml version="1.0"?>
    <?xml-stylesheet href="template.xsl" type="text/xsl" ?>
    <MainChapter RSKM-Language="" EDD-Version="" xmlns:fm="fctfmns.xml">
      <Chapter>
        <Bar/>
        <Head UniqueID="CHDFFEJA">Sicherheit und Gewährleistung</Head>
        <Chapter>
          <Bar/>
          <Head>
            Erklärung der Sicherheitshinweise<MarkIdx fm:MarkerType="Index">Sicherheitshinweise</MarkIdx>
          </Head>
          <Para>
            <BodyText>Alle Sicherheitshinweise in dieser Betriebsanleitung weisen den Bediener frühzeitig auf mögliche Gefahren für Leib und Leben, auf mögliche Schäden an der Maschine und an Beistellgeräten, sowie auf fehlerfreies, produktives Arbeiten hin.</BodyText>
            <BodyText>Alle für den Bediener besonders wichtigen Stellen sind mit einem Warnzeichen versehen. Diese Informationen sind vom Bediener unbedingt zu lesen und zu befolgen.</BodyText>
          </Para>
          <Para>
            <Head>Bedeutungen:</Head>
            <Warning Class="Warning">
              <Warningtable fm:NumRows="2" fm:NumCols="2" fm:ColWidths="5201588,22571176" fm:TblFormat="Warning_Warning">
                <WarningHead fm:NumRows="1">
                  <WarningHeadRow fm:MaxHeight="66060288" fm:MinHeight="0">
                    <Signalword fm:NumCols="2" fm:ColRotation="0">
                      <Graphic>
                        <fm:Frame Type="1" Align="4" Offset="0" BaseLine="374744" Cropped="0" HasImgGroups="0" Pos="18318744,36680121,1204098,1075727,0" Style="15,15,65536,0" Color="Schwarz" Common="0,0,0,2,0,393216,6553600," GroupParent="0">
                          <fm:Inset File="I:\D-140027\D-140027\illustrationen\WARNUNG.eps" Dpi="0" Fixed="1" Flipped="0" Pos="58008,50622,1121246,995520,0" Style="15,15,65536,0" Color="Schwarz" Common="0,0,0,2,2,393216,6553600," GroupParent="0"/>
                        </fm:Frame>
                      </Graphic>WARNUNG
                    </Signalword>
                  </WarningHeadRow>
                </WarningHead>
                <WarningBody fm:NumRows="1">
                  <WarningBodyRow fm:MaxHeight="66060288" fm:MinHeight="0">
                    <WarningSymbol fm:LFTop="" fm:ColRotation="0">
                      <Graphic>
                        <fm:Frame Type="1" Align="4" Offset="0" BaseLine="1579877" Cropped="0" HasImgGroups="0" Pos="7066500,38123845,4629870,2523590,0" Style="15,15,65536,0" Color="Schwarz" Common="0,0,0,2,0,393216,6553600," GroupParent="0">
                          <fm:Inset File="I:\D-140027\D-140027\illustrationen\warning_y_electricity.eps" Dpi="0" Fixed="1" Flipped="0" Pos="980468,0,2668934,2348803,0" Style="15,7,65536,0" Color="Schwarz" Common="0,0,0,2,2,393216,6553600," GroupParent="0"/>
                        </fm:Frame>
                      </Graphic>
                    </WarningSymbol>
                    <WarningMessage fm:LFLeft="" fm:LFTop="" fm:ColRotation="0">
                      <BodyText>Elektrische Spannung! Lebensgefahr durch Stromschlag! Arbeiten an der Maschine, bei denen ein Abschalten des elektrischen Stroms in der Betriebsanleitung zwingend vorgeschrieben ist, muss:</BodyText>
                      <List>
                        <ListEntry>
                          <BodyText>entweder die Stromzuführung durch Abklemmen vom Netz oder Ziehen des Steckers unterbrochen werden,</BodyText>
                        </ListEntry>
                      </List>
                      <BodyText>oder:</BodyText>
                      <List>
                        <ListEntry>
                          <BodyText>der Hauptschalter gegen unbeabsichtigtes Einschalten gesichert werden. Dies kann mit einem Vorhängeschloss oder durch Anbringen einer Plombe am Schalter erfolgen.</BodyText>
                        </ListEntry>
                      </List>
                    </WarningMessage>
                  </WarningBodyRow>
                </WarningBody>
              </Warningtable>
            </Warning>
        </Para>
     </Chapter>
  </Chapter>
</MainChapter>

我正在寻找一个解决方案,以便BodyText中的每个WarningMessage进行循环,如果存在,如果ListEntry中存在List节点,也可以通过每个WarningMessage进行循环{1}}作为子节点,与BodyText一起使用。

每个BodyText都会给出一个WarningMessage,但有时会给出一个List,并且本身可能会有ListEntryBodyTextBodyText }}的

到目前为止,我已经尝试了一些东西,但找不到适合这个问题的解决方案。

以下解决方案不应该...它只会在WarningMessage中获得每个List,但它会忽略 <xsl:template match="//WarningMessage/List" name="WarnList"> <ul> <xsl:for-each select="ListEntry"> <li> <xsl:for-each select="BodyText"> <xsl:value-of select="."/> </xsl:for-each> </li> </xsl:for-each> </ul> </xsl:template> <!-- Warnungen vom Typ "Danger" --> <xsl:template match="Warning[contains(concat(' ', @Class, ' '),' Danger ')]"> <div class="alert alert-danger" role="alert"> <h4><span class="glyphicon glyphicon-warning-sign"/><stong> <xsl:value-of select="Warningtable/WarningHead/WarningHeadRow/Signalword"/> </stong></h4> <xsl:for-each select="Warningtable/WarningBody/WarningBodyRow/WarningMessage/BodyText"> <p> <xsl:value-of select="."/> <xsl:call-template name="WarnList"/> </p> </xsl:for-each> </div> </xsl:template> 个元素。我正在尝试编写一个能够将整个xml句子转换为有效的html / css的xsl:template解决方案。

以下是我得到的内容以及我希望获得的内容:http://i59.tinypic.com/33ma0qo.png

BodyText

是否有人提示如何编写xsl:template来获取每个List,如果存在,还有每个ListEntry每个BodyText? 需要说明的是:一个WarningMessage可能包含一个或多个ListListEntry<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" doctype-system="about:legacy-compat" /> <xsl:template match="/"> <html> <head> <meta charset="utf-8" /> <title>Testausgabe</title> <!-- Bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet"></link> <!-- Individual Theme CSS --> <link href="css/theme.css" rel="stylesheet"></link> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script src="js/jquery.easing.js"></script> </head> <body> <div class="topwrapper"></div> <div class="wrapper"> <div class="container"> <div class="row"> <xsl:apply-templates /> </div><!-- end row --> </div><!-- end container --> </div><!-- end wrapper --> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="js/bootstrap.min.js"></script> <footer class="footer"> <div class="container"> <div class="row"> <img src="images/vc999_logo_94x40.png"></img> </div> </div> </footer> </body> </html> </xsl:template> <xsl:template match="MarkIdx" mode="non-MarkIdx"/> <!-- Header-Bars formatieren --> <xsl:template match="Bar"> <hr/> </xsl:template> <!-- Hauptüberschrift [h1] formatieren --> <xsl:template match="MainChapter/Chapter/Head"> <h1> <xsl:value-of select="."/> </h1> </xsl:template> <!-- Überschrift Ebene 2 [h2] formatieren --> <xsl:template match="MainChapter/Chapter/Chapter/Head"> <h2> <xsl:apply-templates select="." mode="non-MarkIdx"/> </h2> </xsl:template> <!-- Überschrift Ebene 3 [h3] formatieren --> <xsl:template match="//Para/Head | MainChapter/Chapter/Chapter/Chapter/Head"> <h3> <xsl:apply-templates select="." mode="non-MarkIdx"/> </h3> </xsl:template> <!-- Alle BodyText's direkt innerhalb eines Para formatieren --> <xsl:template match="//Para/BodyText"> <p> <xsl:value-of select="."/> </p> </xsl:template> <!-- Listenelemente direkt innerhalb eines Para formatieren --> <xsl:template match="//Para/List"> <ul class="list-group"> <xsl:for-each select="ListEntry"> <xsl:choose> <xsl:when test="position() mod 2 = 0"> <li class="list-group-item"> <xsl:value-of select="BodyText"/> </li> </xsl:when> <xsl:otherwise> <li class="list-group-item list-group-item-grey"> <xsl:value-of select="BodyText"/> </li> </xsl:otherwise> </xsl:choose> </xsl:for-each> </ul> </xsl:template> <!-- Tabellen direkt innerhalb eines Para formatieren --> <xsl:template match="//Para/Table"> <xsl:for-each select="InvisibleTable/TBody"> <table class="table table-striped"> <xsl:for-each select="TRow"> <tr> <xsl:for-each select="TCell"> <td> <xsl:for-each select="BodyText"> <span> <xsl:value-of select="current()"/> </span> <br/> </xsl:for-each> </td> </xsl:for-each> </tr> </xsl:for-each> </table> </xsl:for-each> </xsl:template> <!-- Warnschilder direkt innerhalb eines Para formatieren --> <!-- List's innerhalb einer Warning --> <xsl:template match="//WarningMessage/List" name="WarnList"> <ul> <xsl:for-each select="ListEntry"> <li> <xsl:for-each select="BodyText"> <xsl:value-of select="."/> </xsl:for-each> </li> </xsl:for-each> </ul> </xsl:template> <!-- Warnungen vom Typ "Danger" --> <xsl:template match="Warning[contains(concat(' ', @Class, ' '),' Danger ')]"> <div class="alert alert-danger" role="alert"> <h4><span class="glyphicon glyphicon-warning-sign"/><stong> <xsl:value-of select="Warningtable/WarningHead/WarningHeadRow/Signalword"/> </stong></h4> <xsl:for-each select="Warningtable/WarningBody/WarningBodyRow/WarningMessage/BodyText"> <p> <xsl:value-of select="."/> <xsl:call-template name="WarnList"/> </p> </xsl:for-each> </div> </xsl:template> <!-- Warnungen vom Typ "Warning" --> <xsl:template match="Warning[contains(concat(' ', @Class, ' '),' Warning ')]"> <div class="alert alert-warning" role="alert"> <h4> <span class="glyphicon glyphicon-warning-sign"/> <stong> <xsl:value-of select="Warningtable/WarningHead/WarningHeadRow/Signalword"/> </stong> </h4> <xsl:for-each select="Warningtable/WarningBody/WarningBodyRow/WarningMessage/BodyText"> <p> <xsl:value-of select="."/> </p> </xsl:for-each> </div> </xsl:template> <!-- Warnungen vom Typ "Caution" --> <xsl:template match="//Para/Warning[contains(concat(' ', @Class, ' '),' Caution ')]"> <div class="alert alert-caution" role="alert"> <h4> <span class="glyphicon glyphicon-warning-sign"/> <stong> <xsl:value-of select="Warningtable/WarningHead/WarningHeadRow/Signalword"/> </stong> </h4> <xsl:for-each select="Warningtable/WarningBody/WarningBodyRow/WarningMessage/BodyText"> <p> <xsl:value-of select="."/> </p> </xsl:for-each> </div> </xsl:template> <!-- Infoschilder direkt innerhalb eines Para formatieren --> <xsl:template match="//Para/Info"> <div class="alert alert-info" role="alert"> <h4> <span class="glyphicon glyphicon-info-sign"/> </h4> <xsl:value-of select="./InfoGroup/InfoBody/InfoRow/InfoCell/BodyText"/> </div> </xsl:template> </xsl:stylesheet>

我希望现在很清楚我想要达到的目标......因为我描述我的问题有点困难。

如果澄清很重要,这里是我的整个xslt文件......但我只需要一个解决方案来处理WarningMessage中的List和ListEntrys。

{{1}}

1 个答案:

答案 0 :(得分:1)

只需更改此行:

<xsl:for-each select="Warningtable/WarningBody/WarningBodyRow/WarningMessage/BodyText">

为:

<xsl:for-each select="Warningtable/WarningBody/WarningBodyRow/WarningMessage//BodyText">

唯一的变化是//之前的BodyText。为什么?如果查看输入XML,只有一个BodyText元素是WarningMessage的直接后代。其他人在ListListEntry元素内。而/作为一个分隔符意味着下去&#34;孩子::&#34;轴,//表示查找所有后代 BodyText元素。

然后,输出的相关部分将是:

<p>
  Elektrische Spannung! Lebensgefahr durch Stromschlag! Arbeiten an der Maschine, bei denen ein Abschalten des elektrischen Stroms in der Betriebsanleitung zwingend vorgeschrieben ist, muss:
</p>
<p>entweder die Stromzuführung durch Abklemmen vom Netz oder Ziehen des Steckers unterbrochen werden,
</p>
<p>oder:</p>
<p>der Hauptschalter gegen unbeabsichtigtes Einschalten gesichert werden. Dies kann mit einem Vorhängeschloss oder durch Anbringen einer Plombe am Schalter erfolgen.
</p>

请注意:

  • 对于您的下一个问题,请花点时间发布英文XML样本,而不是德语样本。如果您希望别人帮助您,请不要妨碍他们。
  • 您的代码现在生成合理的输出这一事实并不意味着代码是合理的。如果你愿意的话,我的回答只能解决症状而不是治愈疾病。例如,模板匹配通常不应以//开头,路径表达式不应过长。

编辑(作为对您评论的回复)

Is it also possible to transform the ´List´ elements to ´ul´ tags and the
´ListEntry´ elements to ´li´ tags and display the ´BodyText´ between the ´<li>´
and ´</li>´ tag?

是的,这是可能的。但我建议你重新调整你的代码。从匹配Warning元素的模板开始:

<xsl:template match="Warning[contains(concat(' ', @Class, ' '),' Warning ')]">
    <div class="alert alert-warning" role="alert">
      <h4>
        <span class="glyphicon glyphicon-warning-sign"/>
        <stong>
          <xsl:value-of select="//Signalword"/>
        </stong>
      </h4>
        <xsl:apply-templates/>
    </div>
  </xsl:template>

然后,编写与List匹配的其他模板:

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

匹配ListEntry

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

最后,匹配BodyText

<xsl:template match="BodyText">
  <p>
    <xsl:value-of select="."/>
  </p>
</xsl:template>

如您所见,我不使用for-each,因为它绝对没有必要。相反,所有元素都有单独的模板规则。这应该会为您的代码带来秩序。

相关问题