Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.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文档 <SampleXMLFormat> <Header> <Id>123</Id> </header> <Properties> <property name= "type" value = "a1"> <property name="prop1" value="val1"/> <property name="prop2" value="val2"/> </

我有一个这种格式的xml文档

<SampleXMLFormat>
<Header>
<Id>123</Id>
</header>
<Properties>
<property name= "type" value = "a1">
<property name="prop1" value="val1"/>
<property name="prop2" value="val2"/>
</Properties>
<Properties>
<property name= "type" value = "a2">
<property name="prop1" value="val1"/>
<property name="prop2" value="val2"/>
</Properties>
</SampleXMLFormat>

123
我无法编写xslt转换来将xml文档转换为类似的内容

<SampleXMLFormat>
<Header>
<Id>123</Id>
</Header>
<Properties>
<property name="a1_prop1" value="val1"/>
<property name="a1_prop2" value="val2"/>
<property name="a2_prop1" value="val1"/>
<property name="a2_prop2" value="val2"/>
</Properties>
</SampleXMLFormat>

123

我能得到一些帮助吗???

您的XML格式不正确,但假设这就是您的意思(请注意终止的“type”属性):


那么这个XSLT应该可以做到:

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

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

  <xsl:template match="/*">
    <xsl:copy>
      <Properties>
        <xsl:apply-templates
          select="Properties/*[not(self::property and 
                                   @name = 'type')]" />
      </Properties>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="property/@name">
    <xsl:attribute name="name">
      <xsl:value-of 
        select="concat(../../property[@name = 'type']/@value, '_', .)"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

在上述输入上运行时,结果为:

<SampleXMLFormat>
  <Properties>
    <property name="a1_prop1" value="val1" />
    <property name="a1_prop2" value="val2" />
    <property name="a2_prop1" value="val1" />
    <property name="a2_prop2" value="val2" />
  </Properties>
</SampleXMLFormat>

此转换:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <SampleXMLFormat>
    <xsl:copy-of select="Header"/>
    <Properties>
     <xsl:apply-templates/>
    </Properties>
  </SampleXMLFormat>
 </xsl:template>

 <xsl:template match="property[not(@name='type')]">
  <property name="{../property[@name='type']/@value}_{@name}" value="{@value}"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

应用于提供的XML文档时:

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name= "type" value = "a1"/>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </Properties>
  <Properties>
    <property name= "type" value = "a2"/>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </Properties>
</SampleXMLFormat>
<SampleXMLFormat>
   <Header>
      <Id>123</Id>
   </Header>
   <Properties>
      <property name="a1_prop1" value="val1"/>
      <property name="a1_prop2" value="val2"/>
      <property name="a2_prop1" value="val1"/>
      <property name="a2_prop2" value="val2"/>
   </Properties>
</SampleXMLFormat>

123
生成所需的正确结果:

<SampleXMLFormat>
  <Header>
    <Id>123</Id>
  </Header>
  <Properties>
    <property name= "type" value = "a1"/>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </Properties>
  <Properties>
    <property name= "type" value = "a2"/>
    <property name="prop1" value="val1"/>
    <property name="prop2" value="val2"/>
  </Properties>
</SampleXMLFormat>
<SampleXMLFormat>
   <Header>
      <Id>123</Id>
   </Header>
   <Properties>
      <property name="a1_prop1" value="val1"/>
      <property name="a1_prop2" value="val2"/>
      <property name="a2_prop1" value="val1"/>
      <property name="a2_prop2" value="val2"/>
   </Properties>
</SampleXMLFormat>

123
说明

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <SampleXMLFormat>
    <xsl:copy-of select="Header"/>
    <Properties>
     <xsl:apply-templates/>
    </Properties>
  </SampleXMLFormat>
 </xsl:template>

 <xsl:template match="property[not(@name='type')]">
  <property name="{../property[@name='type']/@value}_{@name}" value="{@value}"/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>
正确使用:

  • s(属性值模板)


  • @AnushaPachunuri,我编辑了这个答案以实现新制定的需求。如果你还有其他的变化,请考虑问一个新问题。这个问题已经完全回答了--两次了.非常感谢。。但我只是删除了我的编辑,因为我自己已经弄明白了。我非常感谢你。是你的答案帮助我弄明白了。你想让我改为编辑你的答案吗?还是重新编辑我的问题。@AnushaPachunuri,不客气。如果您编辑您的问题,请告诉我,我将同步答案。我没有注意到这一点,但为什么我现在在属性下看到一个额外的123。xslt转换可能是错误的。即使你的输出也会给你带来好处that@AnushaPachunuri,复制/粘贴错误--将立即更正。