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,这里是XSLT新手。我有一个XML块,它可以看起来像任何东西,但是基于它的元素名,我需要能够更改它的内容。问题是XML是否可以加前缀 带前缀的XML可能如下所示: <POIS xmlns:tns="http://example.com/integration/docs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <tns:POI> <tns:CTL> &l

这里是XSLT新手。我有一个XML块,它可以看起来像任何东西,但是基于它的元素名,我需要能够更改它的内容。问题是XML是否可以加前缀

带前缀的XML可能如下所示:

<POIS xmlns:tns="http://example.com/integration/docs" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <tns:POI>
      <tns:CTL>
         <tns:transaction_date/>
         <tns:record_qualifier/>
         <tns:start_date>2012-10-12 </tns:start_date>
         <tns:test_indicator>P</tns:test_indicator>
       </tns:CTL>
    </tns:POI>
 </tns:POIS>  

2012-10-12 
P
或不加前缀:

<POIS xmlns="http://example.com/integration/docs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <POI>
      <CTL>
         <transaction_date/>
         <record_qualifier/>
         <start_date>2012-10-12</start_date>
         <test_indicator>P</test_indicator>
    </CTL>
  </POI>
 </POIS>   

2012-10-12
P
当有值时,我想更改以\u date结尾的元素的内容

因此,可能的输出为:

<POIS xmlns:tns="http://example.com/integration/docs" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <tns:POI>
      <tns:CTL>
         <tns:transaction_date/>
         <tns:record_qualifier/>
         <tns:start_date>20121012 </tns:start_date>
         <tns:test_indicator>P</tns:test_indicator>
    </tns:CTL>
   </tns:POI> 
 </tns:POIS>   

20121012
P
这就是我到目前为止所做的:

问题在于,当前缀为XML元素时,它会在更改的元素上使用tns:namespace,或者为非前缀XML元素设置空白名称空间

有什么解决办法吗?这是一个实用程序转换,所以我希望尽可能保持它的通用性

    <xsl:stylesheet version="2.0"      
    xmlns:xp20="http://www.oracle.com/XSL/Transform/
    java/oracle.tip.pc.services.functions.Xpath20" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="node()|@*">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

    <xsl:template match="node()[ends-with(name(), '_date')]">
     <xsl:choose>
      <xsl:when test="(text())" >
             <xsl:element name="{name()}" >
      <xsl:value-of select ="xp20:format-dateTime(text(),'[Y0001][M01][D01]')" /> 
      </xsl:element>
      </xsl:when>
      <xsl:otherwise>
     <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>   
      </xsl:otherwise>
    </xsl:choose>  
       </xsl:template>
    </xsl:stylesheet>


尝试将
更改为

您的XML格式不正确:
应以
结尾,缺少
结束标记,标记上的前缀不一致。请修复您的XML。尝试将
更改为
…是否有帮助?不幸的是,它仍然失败,因为它放置了一个空白的命名空间“20120430”。此外,如果有任何其他方法可以仅更改其中的值,我甚至不需要添加元素。我能够修复此问题: