Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT将替换元素转换为属性_Xml_Xslt - Fatal编程技术网

Xml XSLT将替换元素转换为属性

Xml XSLT将替换元素转换为属性,xml,xslt,Xml,Xslt,给定一个,我想提取一个元素的值,将其转换为给定的对应值,并将其作为属性值插入第二个元素的值旁边 我有以下XML(修剪过多的标记、命名空间等): 将其添加到有效负载后: <xsl:apply-templates select="post_parent" /> 其中以前是245(现在是NewValue)。由于post\u name在发布的XML中是post\u parent的前辈,因此可以使用前辈轴从当前post\u parent上下文获取它: <xsl:template mat

给定一个
,我想提取一个元素的值,将其转换为给定的对应值,并将其作为属性值插入第二个元素的值旁边

我有以下XML(修剪过多的标记、命名空间等):

将其添加到
有效负载后:

<xsl:apply-templates select="post_parent" />

其中
以前是
245
(现在是
NewValue
)。

由于
post\u name
在发布的XML中是
post\u parent
的前辈,因此可以使用
前辈
轴从当前
post\u parent
上下文获取它:

<xsl:template match="post_parent[.='245']">
  <TestPart Container="NewValue/{preceding-sibling::post_name}" />
</xsl:template>


另外,您的
可以与
一起替换为
xsl:apply templates
。和foreach循环相比,在XSLT中使用模板被认为是更自然的方法。

“我有一个翻译值列表”这个列表在哪里,它看起来像什么?请将其添加到您的问题中,并向我们展示您希望获得的确切输出。@michael.hor257k感谢您的评论。转换后的值在很大程度上是不相关的,我需要帮助的是转换和连接两个元素的值的过程。har07已经给了我一个完美的解决方案。“har07给了我一个完美的解决方案”耸耸肩。真为你高兴。我还是不明白这个问题。特别是对
245
值进行硬编码的部分。该项从一个ID为(例如245)的系统中导出,并将以新ID(无论是什么)导入到新系统中。图例。这正是我需要知道的。我今天才开始学习XSLT,所以我会慢慢地学习。快速跟进:我是否必须为每个
post\u父项
值创建一个模板(样本为245,但会有十几个),或者是否有更快的方法进行替换(例如关键值情况)?不管怎样,我在谷歌上搜索到了一个我需要的样本。再次感谢。
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
  <Container>
    <xsl:for-each select ="items/item">
      <Data>
        <Item Id="{post_name}" Status="{status}">
        <Route Alias="{post_name}" />
        <Details Owner="{creator}" />
        <Title Title="{title}" />
      </Data>
    </xsl:for-each>
  </Container>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="post_parent/text()[.='245']">
  <TestPart Container="NewValue" />
</xsl:template>
<xsl:apply-templates select="post_parent" />
<TestPart Container="NewValue" />
<TestPart Container="NewValue/{post_name}" />
<xsl:template match="post_parent[.='245']">
  <TestPart Container="NewValue/{preceding-sibling::post_name}" />
</xsl:template>
<xsl:template match="post_parent[.='245']">
  <TestPart Container="NewValue/{parent::*/post_name}" />
</xsl:template>