简体   繁体   English

使用XSLT从XML节点检索文本

[英]Retrieving text from XML node with XSLT

I'm sorry if this seems like an incredibly basic question, but I would really (and I mean really) appreciate any help I can get. 如果这看起来像一个非常基本的问题,我很抱歉,但我真的(我的意思是真的)感谢我能得到的任何帮助。 I'm simply trying to do the following: 我只是想做以下事情:
1. Replace the self-closing with 1.更换自动关闭
2. Grab the text from 'Second_node' 2.从'Second_node'抓取文本
3. Store that text in a variable 3.将该文本存储在变量中
4. Place that text inside in the new 'Seventh_node'. 4.将该文本放入新的“Seventh_node”中。

I've accomplished step 1, but I can't seem to retrieve the necessary information from the required element. 我已经完成了第1步,但我似乎无法从所需元素中检索必要的信息。 I've included three examples below as well as my working XSLT. 我在下面列举了三个例子以及我正在使用的XSLT。 I guess the key problem is storing the text contents of 'Second_node' and placing it in the new element. 我想关键问题是存储'Second_node'的文本内容并将其放在新元素中。 By way of addition info, I'm using Saxon 6.5 for the transform. 通过添加信息,我使用Saxon 6.5进行转换。 If the information provided is in anyway incomplete, please let me know. 如果提供的信息仍然不完整,请告诉我。

THANK YOU! 谢谢!

The source XML: 源XML:

<firstnode>
  <Second_node>text for second node</Second_node>
   <Third_node>
     <Fourth_node>
       <Fifth_node>text for fifth node</Fifth_node>
       <Sixth_node>text for sixth node</Sixth_node>
        <Seventh_node />
   </Fourth_node>
 </Third_node>
</firstnode>

What I have so far: 到目前为止我所拥有的:

<firstnode>
  <Second_node>text for second node</Second_node>
   <Third_node>
     <Fourth_node>
       <Fifth_node>text for fifth node</Fifth_node>
       <Sixth_node>text for sixth node</Sixth_node>
        <Seventh_node></Seventh_node>
   </Fourth_node>
   </Third_node>
</firstnode>

What I need: 我需要的:

<firstnode>
  <Second_node>text for second node</Second_node>
   <Third_node>
     <Fourth_node>
       <Fifth_node>text for fifth node</Fifth_node>
       <Sixth_node>text for sixth node</Sixth_node>
        <Seventh_node>text for second node</Seventh_node>
   </Fourth_node>
   </Third_node>
</firstnode>

My XSLT so far: 到目前为止我的XSLT:

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


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

        <xsl:text>text for second node</xsl:text>
    </xsl:copy>
</xsl:template>

You could try just placing the text from the second node into a variable, then use that variable to place that text further down in the seventh node. 您可以尝试将第二个节点中的文本放入变量中,然后使用该变量将该文本放在第七个节点中。

<xsl:variable name="VAR_SecNode">
   <xsl:value-of select="Second_node"/>
</xsl:variable>

...
<Seventh_node><xsl:value-of select="$VAR_SecNode" /></Seventh_node>
...

I do not see the need for an variable. 我认为不需要变量。
Try this: 尝试这个:

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

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

    <xsl:template match="Seventh_node">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:value-of select="//Second_node"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

The <xsl:apply-templates select="@*"/> will copy attribute of Seventh_node. <xsl:apply-templates select="@*"/>将复制Seventh_node的属性。 The <xsl:apply-templates select="node()"/> will copy child nodes of Seventh_node. <xsl:apply-templates select="node()"/>将复制Seventh_node的子节点。 If you do not need that remove this lines. 如果你不需要删除这一行。

You can update your second template to the following: 您可以将第二个模板更新为以下内容:

<xsl:template match="Seventh_node/text()">
    <xsl:text>text for second node</xsl:text>
</xsl:template>

This will match only the text of your Seventh_node and replace it with whatever you put in the <xsl:text> element. 这将仅匹配您的Seventh_node的文本,并将其替换为您放在<xsl:text>元素中的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM