2013-03-15 102 views
0

我有以下XMLXSLT刪除節點和atrribute值轉換爲元素名稱

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <lst name="somename"> 
     <node1></node1> 
     <node2></node2> 
    </lst> 
    <result name="somename" count="5"> 
     <doc> 
      <str name="NodeA">ValueA</str> 
      <str name="NodeB">ValueB</str> 
      <str name="NodeC">ValueC</str> 
     </doc> 
     <doc> 
      <str name="NodeA">ValueD</str> 
      <str name="NodeB">ValueE</str> 
      <str name="NodeC">ValueF</str> 
     </doc> 
    </result> 
</response> 

,我要轉換爲

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <doc> 
     <NodeA>ValueA</NodeA> 
     <NodeB>ValueB</NodeB> 
     <NodeC>ValueC</NodeC> 
    </doc> 
    <doc> 
     <NodeA>ValueD</NodeA> 
     <NodeB>ValueE</NodeB> 
     <NodeC>ValueF</NodeC> 
    </doc> 
</response> 

正如你所看到的LST節點被完全去除,並屬性值現在成爲節點。

首先,我用這個xslt代碼去除了第一個節點。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="lst"/> 
</xsl:stylesheet> 

這給了我這個

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <result name="somename" count="5"> 
     <doc> 
      <str name="NodeA">ValueA</str> 
      <str name="NodeB">ValueB</str> 
      <str name="NodeC">ValueC</str> 
     </doc> 
     <doc> 
      <str name="NodeA">ValueD</str> 
      <str name="NodeB">ValueE</str> 
      <str name="NodeC">ValueF</str> 
     </doc> 
    </result> 
</response> 

然後用這個XSLT從鏈接[link] Convert attribute value into element

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="response/result/doc"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="token"> 
     <xsl:element name="{@name}"> 
      <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

但它並沒有幫助。它給了我這個。

<?xml version="1.0" encoding="utf-8"?> 
<doc>ValueAValueBValueC</doc> 
<doc>ValueDValueEValueF</doc> 

請幫我把屬性值轉換成節點的第二部分。 是否有可能讓一個xslt做這兩件事?

+0

問題是什麼/你在哪裏卡住了? – 2013-03-15 14:00:22

+0

您可以展示迄今爲止獲得的XSLT,因此我們可以看到/指出問題到底是什麼? – 2013-03-15 14:03:27

+0

發佈更新。請檢查。 – user1677271 2013-03-15 14:14:10

回答

1

你可以在這裏實現你所有的目標,一個XSLT這是非常接近目前的一個:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:strip-space elements="*" /> 

    <!-- copy everything as-is apart from exceptions below --> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*"/> 
     </xsl:copy> 
    </xsl:template> 

    <!-- delete lst --> 
    <xsl:template match="lst"/> 

    <!-- strip out the result element but still include its children --> 
    <xsl:template match="result"> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <!-- convert str name="X" to X --> 
    <xsl:template match="str"> 
     <xsl:element name="{@name}"> 
     <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

完全按照我想要給伊恩的方式工作。特別感謝您在每行代碼之前的自我解釋評論。 – user1677271 2013-03-18 11:34:16

+0

一個奇怪的問題。 當我們在源xml中有一個空節點時,xslt會將這個節點的結束標記放在下一行,而不是同一行。 因此,當導入這樣的文件時,我們在數據中有新的行字符。 例如,這 \t 轉化爲 \t \t 但是應該理想已tranformed要麼這 \t 或該 \t 任何解決方案對於這個問題? – user1677271 2013-03-18 12:31:37

+0

@ user1677271如果節點真正完全是空的,只要它是負責解析原始XML文件的XSLT處理器,''應該處理該節點。如果您將XSLT處理程序交給預解析的DOM文檔,而不是讓它解析XML,那麼它可能不會去除僅包含空白的文本節點。你可以通過添加一個'' – 2013-03-18 13:19:42

0

你可以以此爲基礎

XSL如何將所有元素複製除了那些所謂的註釋

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0"> 

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

    <!-- copy xsd:annotation and children, but don't copy the attributes --> 
    <xsl:template match="xsd:annotation"> 

     <xsd:annotation> 
      <xsl:copy-of select="*"/> 
     </xsd:annotation> 

    </xsl:template> 

</xsl:stylesheet> 
0

這將做到這一點:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="node()"> 
    <xsl:copy> 
     <xsl:apply-templates /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="lst"/> 

    <xsl:template match="str[@name]"> 
    <xsl:element name="{@name}"> 
     <xsl:apply-templates /> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

當你的樣品輸入運行,這將產生:

<response> 
    <result> 
    <doc> 
     <NodeA>ValueA</NodeA> 
     <NodeB>ValueB</NodeB> 
     <NodeC>ValueC</NodeC> 
    </doc> 
    <doc> 
     <NodeA>ValueD</NodeA> 
     <NodeB>ValueE</NodeB> 
     <NodeC>ValueF</NodeC> 
    </doc> 
    </result> 
</response>