2014-07-10 49 views
0

我想用xslt替換元素標記名稱。我有一個輸出這樣的:如何使用xslt更改父標記中元素的文本

<gl-cor:documentInfo> 
    <gl-cor:entriesType contextRef="journal_context">DocumentID</gl-cor:entriesType> 
    <gl-cor:uniqueID contextRef="journal_context">RevisionID</gl-cor:uniqueID> 
</gl-cor:documentInfo> 
<gl-cor:entityInformation> 
    <gl-cor:entityPhoneNumber> 
     <gl-cor:phoneNumber contextRef="journal_context">779633</gl-cor:phoneNumber> 
    </gl-cor:entityPhoneNumber> 
    <gl-cor:entityFaxNumberStructure> 
     <gl-cor:entityFaxNumbercontextRef="journal_context">1234-56-89</gl-cor:entityFaxNumber> 
    </gl-cor:entityFaxNumberStructure> 
</gl-cor:entityInformation> 

而且,我想我的輸出是這個樣子的:

<gl-cor:documentInfo> 
    <gl-cor:entriesType contextRef="journal_context">DocumentID</gl-cor:entriesType> 
    <gl-bus:uniqueID contextRef="journal_context">RevisionID</gl-cor:uniqueID> 
</gl-cor:documentInfo> 
<gl-cor:entityInformation> 
    <gl-bus:entityPhoneNumber> 
     <gl-bus:phoneNumber contextRef="journal_context">779633</gl-bus:phoneNumber> 
    </gl-bus:entityPhoneNumber> 
    <gl-bus:entityFaxNumberStructure> 
     <gl-bus:entityFaxNumbercontextRef="journal_context">1234-56-89</gl-bus:entityFaxNumber> 
    </gl-bus:entityFaxNumberStructure> 
</gl-cor:entityInformation> 

所有的<gl-cor:entityInformation>兒童應更換,而不是GL-COR,它應該是GL -總線。是否有可能做到這一點?

我試着創建一個示例xslt,但它沒有工作。該錯誤發生在<gl-bus:phoneNumber>,因爲我認爲它包含特殊字符?像「 - 」和「:」。

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

<xsl:template match="gl-cor:entityInformation/gl-cor:entityPhoneNumber/gl-cor:phoneNumber"> 
<gl-bus:phoneNumber> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</gl-bus:phoneNumber> 
</xsl:template> 

有人可以幫我解決這個問題嗎?非常感謝。

回答

0

首先gl-corgl-bus是命名空間前綴。名稱空間前綴寫在XML元素之前,並與:分離。所以,你的問題不是因爲字符-:,這些都是有效的字符,請仔細閱讀這些文章/教程:

http://www.w3schools.com/xml/xml_namespaces.asp http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML

要回答你的問題,我們需要知道什麼namspace的URI對於gl-corgl-bus,但它應該是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gl-cor="http://example.org/gl-cor" xmlns:gl-bus="http://example.org/gl-bus"> 
    <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="*[ancestor::gl-cor:entityInformation]"> 
     <xsl:element name="gl-bus:{local-name()}"> 
      <xsl:apply-templates select="@*|node()" /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

模板*[ancestor::gl-cor:entityInformation]將匹配所有的(大)的兒童gl-cor:entityInformation

注意

在XSLT的命名空間應該更新,並匹配您輸入XML:

xmlns:gl-cor="http://example.org/gl-cor" 
xmlns:gl-bus="http://example.org/gl-bus" 
+0

感謝您的回答。我已經使用你的代碼,當我將xml輸出轉換爲xsl時,錯誤指向了這個部分:gl-bus:{local-name()},我不知道它是什麼意思。 – hannah

+0

@hannah:更新您的問題併發布更多您的輸入XML,因爲您需要設置正確的名稱空間。也給我們你收到的確切的錯誤。 –

+0

哇!我忘了更改xslt中的命名空間。而且,是的,它工作正常。非常感謝。 – hannah