2016-02-21 59 views
3

我有這樣的XML:XSLT 2.0:正則表達式提取物和修改元素值

<xml> 
    <row> 
     <image><![CDATA[javascript: open_window_zoom('http://example.com/image.php?image=/images/test/example.png&pID=46391&download=noid_90.png&name=Test name', 975, 366);]]></image> 
     <quantity>0</quantity> 
    </row> 
    <row> 
     <image><![CDATA[javascript: open_window_zoom('http://example.com/image.php?image=/images/test/another.png&pID=06395&download=anotherfile.png&name=Test name', 975, 366);]]></image> 
     <quantity>0</quantity> 
    </row> 
</xml> 

它可以提取pID=NUMBERHERE&download=FILENAMEHERE.png(之前它添加新的URL)從<image>元素?

輸出應該是這樣的:

<xml> 
    <row> 
     <image>http://newurl.com/pID=46391&download=noid_90.png</image> 
     <quantity>0</quantity> 
    </row> 
    <row> 
     <image>http://newurl.com/pID=06395&download=anotherfile.png</image> 
     <quantity>0</quantity> 
    </row> 
</xml> 

我嘗試了一些東西,但我不能得到desidered結果。用於起動I可以複製當前結構:

<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="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+1

什麼'(?<=)具有PID [^&'] +'?匹配'&pID'後面的所有內容,直到達到'&'或'''。不包括最初的&&。 –

回答

2

此XSLT 2.0樣式表分裂由&的值,然後使用謂詞只過濾那些matches()所提供的正則表達式參數名稱是pID或下載。使用@separator的XSL:價值與&加盟值:

<xsl:stylesheet version="2.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="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="image"> 
     <xsl:copy> 
      <xsl:text>http://newurl.com/</xsl:text> 
      <xsl:value-of select="tokenize(., '&amp;')[matches(., '(pID|download).*')]" 
          separator="&amp;"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

另外,這個XSLT 2.0樣式表使用xsl:analyze-string提取所提供的正則表達式匹配的文本:

<xsl:stylesheet version="2.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="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="image"> 
     <xsl:copy> 
      <xsl:text>http://newurl.com/</xsl:text> 
      <xsl:analyze-string select="." regex=".*(pID=.*&amp;download=.*)&amp;.*"> 
       <xsl:matching-substring> 
        <xsl:value-of select="regex-group(1)"/> 
       </xsl:matching-substring> 
       <xsl:non-matching-substring></xsl:non-matching-substring> 
      </xsl:analyze-string> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
+0

謝謝,兩種解決方案都很棒。還有一個問題:請給我寫一個例子來說明如何在輸出中將download =參數更改爲image =? – Adrian

2

嘗試此XSLT:

<xsl:stylesheet version="1.0" 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="image"> 
    <xsl:copy> 
     <xsl:value-of select="concat('http://newurl.com/pID=', substring-before(substring-after(text(), '&amp;pID='), '&amp;name='))"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet>