2012-03-30 54 views
1

考慮XML文件作爲如何創建訪問性能的XSL文件文件,以便爲XML到XML文件轉換

<Content> 
    <abc>....</abc> 
</Content> 

我有一個特性與假設

abc=def 

和文件我最終轉換XML文件看起來像這transformes第一個XML文件,建議立即進行刪除

<Content> 
    <def>....</def> 
</Content> 

所以我的XSL文件d使用上述屬性文件並對其進行轉換。任何人都可以建議我們如何使用XSLT實現這一目標?

+2

無法單獨使用XSLT訪問非XML文件。你有什麼其他的編程語言?此外,**你有什麼嘗試?** – Tomalak 2012-03-30 07:04:38

回答

0

如果存儲屬性文件的XML格式,例如

<Properties> 
    <Property value="abc">def</Property> 
    <Property value="...">...</Property> 
</Properties> 

那麼你可以使用XSLT同時處理XML文件,並用高清的人代替ABC元素,等等。

例如

<?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:variable name="props" select="document('Properties.xml')"/> 

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

    <xsl:template match="Content/*"> 
     <xsl:variable name="repl" select="$props/Properties/Property[@value=name(current())]"/> 
     <xsl:choose> 
      <xsl:when test="$repl"> 
       <xsl:element name="{$repl}"> 
        <xsl:apply-templates/> 
       </xsl:element> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:copy> 
        <xsl:apply-templates/> 
       </xsl:copy> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

當施加到

<?xml version="1.0" encoding="UTF-8"?> 
<Content> 
    <abc>xxx</abc> 
    <def>zzz</def> 
    <ghi>ccc</ghi> 
</Content> 

與屬性文件

<?xml version="1.0" encoding="UTF-8"?> 
<Properties> 
    <Property value="abc">def</Property> 
    <Property value="def">www</Property> 
</Properties> 

這導致

<?xml version="1.0" encoding="UTF-8"?> 
<Content> 
    <def>xxx</def> 
    <www>zzz</www> 
    <ghi>ccc</ghi> 
</Content> 

加成由傑文

------------------------------------------------------------------------------------ 
     My question is that if i want to access 
     'value' attribute(input xml file tags) 

    ex:  
        <Property value="abc">def</Property> 

我想「ABC」,以在一些其它變量properties.xml文件訪問說「repl1」,使用類似「$道具/屬性/ ..」的選擇? 這怎麼能實現。

回答Maestro13

還不清楚你想要做的事情,所以我只會給你一些提示,這是希望有用。

value屬性可以通過Xpath表達式來訪問,包括/@value。你當然需要有一個當前的Property節點。這可以通過任一做一個循環如下進行:

<xsl:for-each select="$props/Properties/Property"> 
    <w><xsl:value-of select="@value"/></w> 
</xsl:for-each> 

在環路內的當前節點是一個環繞在,和@value作用在該節點上。
或者你也可以定義一個模板做同樣的事情(一個模板,除非命名和直接調用,將​​被所有匹配的節點重複調用)。
另一種方式來檢索屬性的值爲首先通過選擇第n發生具有XPath表達式,其選擇恰好一個Property元素,例如如下:

<xsl:value-of select="$props/Properties/Property[3]/@value"/> 

在上述屬性的情況下文件,此將返回ghi

+1

你的身份模板錯了。 – Tomalak 2012-03-30 07:53:14

+0

@ Maestro13:我們不能在Properties.xml文件中使用其他變量(比如'repl1')中的'value'屬性,在select中使用類似'$ props/Properties/..'的東西嗎? – 2012-04-03 05:20:30

+0

@ Maestro13:你可以儘快給我解決方案。提前致謝。 – 2012-04-03 05:30:51