English 中文(简体)
恢复节点
原标题:Restructing nodes

我想与遗属价值相匹配,如果同一价值出现在另一个分母中,即两者必须合并。 例如,

<xml>
   <title>
   <metadata>
    <ref cite="ABC" relevance="2"/>
    </metadata>
     <body>
       <para>
            <text>(some text from title)</text>
      </para>
     </body>
    </title>
    <title>
      <metadata>
         <ref cite="ABC" relevance="1"/>
      </metadata>
     <body>
       <para>
            <text>(some more text from title 2)</text>
      </para>
     </body>
    </title>

    <mainbody>
        <targetref cite="ABC"/>
        <text>This is a text</text>  
   </mainbody>
 </xml>

因此,在进行转换之后,产出应当如下,因此,根据要素标题/元数据/参考和相关性(1,2....)的“ABC”号,如果“targetref”项的内容与“ref”项的内容相匹配,则标题的内容并入了主文的内容。

<xml>
  <mainbody>
    <targetref cite="ABC"/>
    <text>This is a text 
        <para><text>(some more text from title 2)</text></para>
       <para><text>(some text from title)</text></para> 
    </text>
   </mainbody>
 </xml> 

成就

问题回答

在这里,你想要的东西是:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output omit-xml-declaration="no" method="xml"/>
<xsl:strip-space elements="*"/>

<xsl:template match="mainbody">
<xsl:copy>
        <xsl:copy-of select="targetref"/>
        <xsl:element name="text">
            <xsl:value-of select="."/>
            <xsl:variable name="key" select="targetref/@cite"/>
            <xsl:for-each select="/xml/title[metadata/ref/@cite=$key]">
                <xsl:sort data-type="number" select="metadata/ref/@relevance" order="ascending" />
                <xsl:copy-of select="body/para"/>
            </xsl:for-each>
        </xsl:element>
    </xsl:copy>
</xsl:template>

<xsl:template match="xml/title"/>

<xsl:template match="xml">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

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

</xsl:stylesheet>

trick笑是要忽视正常流动的节点xml/ 标题,我们把注意力放在主角上,然后我们呼吁大家选择和命令必要的节点,并在我们想要的主人内部撰写信息。 甄选需要一个变数(关键)来过滤,因为主人对此有具体规定

这样做的一个途径是,根据元数据对<><><>><><>>记录进行检索。

<xsl:key name="ref" match="title" use="metadata/ref/@cite" />

然后,假定你在主人中的text要素上的位置,你可以把他们看上去做,同时进行分类。

<xsl:apply-templates select="key( ref , preceding-sibling::targetref/@cite)/body/para">
   <xsl:sort select="../../metadata/ref/@relevance" />

这里是全部的锡索托

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:key name="ref" match="title" use="metadata/ref/@cite" />
   <xsl:template match="title" />

  <xsl:template match="mainbody/text">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:apply-templates select="key( ref , preceding-sibling::targetref/@cite)/body/para" >
         <xsl:sort select="../../metadata/ref/@relevance" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

在抽样XML时,以下是产出:

<xml>
    <mainbody>
        <targetref cite="ABC"/>
        <text>This is a text
            <para>
                <text>(some more text from title 2)</text>
            </para>
            <para>
                <text>(some text from title)</text>
            </para></text>
    </mainbody>
</xml>




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签