【问题标题】:Extract substring using XSLT / XPath using XSLT Transformation (to use in WSO2)使用 XSLT 提取子字符串/使用 XSLT 转换的 XPath(在 WSO2 中使用)
【发布时间】:2014-09-29 20:38:00
【问题描述】:

我是 XSLT / XPath 的新手 ....

我有以下 XML 消息...

<?xml version="1.0" encoding="utf-8"?>
 <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
  <soapenv:Body>
   <request xmlns="http://ws.apache.org/ns/synapse">
    <gam:process xmlns:gam="http://gamopera.ifpl.csi.it">
     <!--Zero or more repetitions:-->
     <gam:id>1</gam:id>
     <gam:id>3</gam:id>
     <gam:id>5</gam:id>
     <gam:id>7</gam:id>
     <gam:id>438</gam:id>
     <gam:id>2</gam:id>
     <gam:id>4</gam:id>
     <gam:id>6</gam:id>
     <gam:id>8</gam:id>
    </gam:process>
    <ax2586:good xmlns:ax2586="http://gamopera.ifpl.csi.it">
     <ax2586:id>1</ax2586:id>
     <ax2586:id>3</ax2586:id>
     <ax2586:id>5</ax2586:id>
     <ax2586:id>7</ax2586:id>
    </ax2586:good>
   </request>
  </soapenv:Body>
 </soapenv:Envelope>

我想得到这样的东西

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
 <soapenv:Body>
  <ns:processResponse xmlns:ns="http://gamopera.ifpl.csi.it">
   <ns:return xsi:type="ax2586:Result" xmlns:ax2586="http://dto.gamopera.ifpl.csi.it/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <ax2586:good xsi:type="ax2586:ElencoId" xmlns:ax2586="http://gamopera.ifpl.csi.it">
           <ax2586:good>
              <ax2586:id>1</ax2586:id>
              <ax2586:id>3</ax2586:id>
              <ax2586:id>5</ax2586:id>
              <ax2586:id>7</ax2586:id>
           </ax2586:good>
        </ax2586:good>
        <ax2586:message>Message Store does not exist.</ax2586:message>
        <ax2586:statusCode>0</ax2586:statusCode>
        <ax2586:wrong xsi:type="ax2586:ElencoId">
           <ax2586:wrong xmlns:ax2586="http://gamopera.ifpl.csi.it">
              <gam:id xmlns:gam="http://gamopera.ifpl.csi.it" xmlns="http://ws.apache.org/ns/synapse">438</gam:id>
              <gam:id xmlns:gam="http://gamopera.ifpl.csi.it" xmlns="http://ws.apache.org/ns/synapse">2</gam:id>
              <gam:id xmlns:gam="http://gamopera.ifpl.csi.it" xmlns="http://ws.apache.org/ns/synapse">4</gam:id>
              <gam:id xmlns:gam="http://gamopera.ifpl.csi.it" xmlns="http://ws.apache.org/ns/synapse">6</gam:id>
              <gam:id xmlns:gam="http://gamopera.ifpl.csi.it" xmlns="http://ws.apache.org/ns/synapse">8</gam:id>
           </ax2586:wrong>
        </ax2586:wrong>
     </ns:return>
  </ns:processResponse>

现在在网上搜索我已经构建了这个 XSLT 转换

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="2.0">
  <xsl:template match="/">
   <xsl:variable name="ns1" select="//*[local-name()='process']/*"/>
   <xsl:variable name="ns2" select="//*[local-name()='good']/*"/>
   <xsl:variable name="difference" select="$ns1[not(.=$ns2)],$ns2[not(.=$ns1)]"/>
   <ax2586:wrong xmlns:ax2586="http://gamopera.ifpl.csi.it">
     <ax2586:wrong><xsl:copy-of select="$difference"/></ax2586:wrong>
   </ax2586:wrong>
  </xsl:template>
</xsl:stylesheet> 

这似乎工作正常,但尝试在网络上的不同 XSLT 测试器中使用它我发现它在某处工作而在某处不工作,所以我不太确定这是否是正确且可移植的解决方案。

注意:我必须在 WSO2 代理中使用它

有什么建议或替代方法可以获得相同的结果吗?

非常感谢您

切萨雷

【问题讨论】:

标签: xml xslt xpath wso2


【解决方案1】:

xsltcake.com 允许您使用不同的引擎运行 XSLT。如果您选择了 .NET XSLT 引擎,则会出现以下错误

表达式的预期结尾,找到','。 System.Xml.Xsl.XslTransformException

这可能属于的唯一位置是以下 XPath

$ns1[not(.=$ns2)],$ns2[not(.=$ns1)]

这在 XSLT 1.0 中肯定是不允许的。看来你的意思

$ns1[not(.=$ns2)]

在这种情况下,它确实有效。直播版:http://www.xsltcake.com/slices/3ams3F

【讨论】:

  • 你的意思是 XPath $ns1[not(.=$ns2)],$ns2[not(.=$ns1)] 是关于 XSLT 2.0 的吗?在任何情况下,我都看到您的实时版本在 xsltcake.com 中运行....谢谢!
  • @Cesare XPath 2.0 to be exact。 (另外,如果有帮助,请考虑接受答案。)
  • 我在上面的所有引擎中都尝试过使用相同的 XPath $ns1[not(.=$ns2)],现在它似乎无处不在
猜你喜欢
  • 2014-05-05
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多