2010-07-22 46 views
1

只是一個簡單的問題。我有一個XML,我希望只改變它的一部分而不改變其他任何東西。這裏是什麼,我希望做一個簡單的例子:XSLT一種XML,除了一個部分外,其餘部分都是相同的

輸入:

<?xml version="1.0" encoding="UTF-8"?> 
<dita xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../xsd/ditabase.xsd"> 
    <topic id="abc"> 
     <title>Sample XML</title> 
     <body> 
     <section id="a"> 
      <p> Hello section A </p> 
     </section> 
     <section id="b"> 
      <p> General Content </p> 
     </section> 
     <section id="c"> 
      <p> Hi thank you for coming from $state </p> 
     </section> 
     </body> 
    </topic> 
</dita> 

輸出

<?xml version="1.0" encoding="UTF-8"?> 
<dita xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../xsd/ditabase.xsd"> 
    <topic id="abc"> 
     <title>Sample XML</title> 
     <body> 
     <section id="a"> 
      <p> Hello section A </p> 
     </section> 
     <section id="b"> 
      <p> General Content </p> 
     </section> 
     <section id="c" audience = "WA"> 
      <p> Hi thank you for coming from WA </p> 
     </section> 
      <section id="c" audience = NY"> 
      <p> Hi thank you for coming from NY </p> 
     </section> 
      <section id="c" audience = "AL"> 
      <p> Hi thank you for coming from AL </p> 
     </section> 
      <section id="c" audience = "GA"> 
      <p> Hi thank you for coming from GA </p> 
     </section> 
      ... 
      <!--Continue for the rest of the states--> 
      ... 
     </body> 
    </topic> 
</dita> 

我使用XALAN處理器如果能夠有所幫助。非常感謝提前:d

+1

你是否想在'

「內替換'$ state',或者在你的輸入文檔中出現的地方? – 2010-07-22 15:00:30

+0

是的我只看到

內沒有替換$ state,我希望輸入可以來自XSL文件而不是XML。 – Bilzac 2010-07-22 15:12:01

+1

好問題(+1)。查看我的答案獲得完整的解決方案。 – 2010-07-22 16:34:26

回答

2

我建議你改變一點點的XML的格式,使solurion簡單:

而不是

<p> Hi thank you for coming from $state </p> 

使用

<p> Hi thank you for coming from <state/> </p> 

對於這種格式,下面變換

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:my="my:my" 
> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<my:states> 
    <state name="WA"/> 
    <state name="NY"/> 
    <state name="AL"/> 
    <state name="GA"/> 
</my:states> 

<xsl:variable name="vStates" select="document('')/*/my:states/*"/> 

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

<xsl:template match="section[descendant::state]"> 
    <xsl:variable name="vSect" select="."/> 

    <xsl:for-each select="$vStates"> 
    <xsl:apply-templates select="$vSect" mode="generate"> 
     <xsl:with-param name="pState" select="@name"/> 
    </xsl:apply-templates> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="section" mode="generate"> 
    <xsl:param name="pState"/> 

    <xsl:copy> 
    <xsl:copy-of select="@*"/> 
    <xsl:attribute name="audience"> 
    <xsl:value-of select="$pState"/> 
    </xsl:attribute> 
    <xsl:apply-templates> 
    <xsl:with-param name="pState" select="$pState"/> 
    </xsl:apply-templates> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="state"> 
    <xsl:param name="pState"/> 
    <xsl:value-of select="$pState"/> 
</xsl:template> 
</xsl:stylesheet> 

當在(略有修改)應用提供的XML文檔

<dita xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../xsd/ditabase.xsd"> 
    <topic id="abc"> 
     <title>Sample XML</title> 
     <body> 
     <section id="a"> 
      <p> Hello section A </p> 
     </section> 
     <section id="b"> 
      <p> General Content </p> 
     </section> 
     <section id="c"> 
      <p> Hi thank you for coming from <state/> </p> 
     </section> 
     </body> 
    </topic> 
</dita> 

產生想要的,正確的結果

<dita xsi:noNamespaceSchemaLocation="../../../xsd/ditabase.xsd" xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <topic id="abc"> 
     <title>Sample XML</title> 
     <body> 
     <section id="a"> 
      <p> Hello section A </p> 
     </section> 
     <section id="b"> 
      <p> General Content </p> 
     </section> 
     <section id="c" audience="WA"> 
      <p> Hi thank you for coming from WA </p> 
     </section> 
<section id="c" audience="NY"> 
      <p> Hi thank you for coming from NY </p> 
     </section> 
<section id="c" audience="AL"> 
      <p> Hi thank you for coming from AL </p> 
     </section> 
<section id="c" audience="GA"> 
      <p> Hi thank you for coming from GA </p> 
     </section> 
     </body> 
    </topic> 
</dita> 
+0

非常感謝!這真的有幫助。你提出的改變是完全合理的。使用$ state只是一個沒有具體的建議! – Bilzac 2010-07-22 17:51:29

+1

真正優雅的解決方案! – 2010-07-22 23:29:50

相關問題