2016-07-12 57 views
0

我們想要使用的傳統XML提要(它與特定數據庫耦合並且沒有XSD)發出「Y」或「N」作爲真值。我們正在創建一個XSD並自動生成C#類,並進行了一些轉換,以使其更加整潔。將XML「是/否」字符串轉換爲布爾值

因此,如果我在對象上有像<IsFed>Y</IsFed>這樣的字段,那麼如何使用XSLT對其進行轉換,以便與xsd:boolean進行驗證?

我感興趣的兩種方法:

  1. 明確列出每個字段進行改造
  2. 自動檢測每一個這樣的是/否字段(我知道這可能有誤差)

示例XML可能看起來像這樣:

<Animal type="hamster"> 
<IsFed>Y</IsFed> 
<Name>Gerald</Name> 
</Animal> 
<Animal type="cow"> 
<IsFed>N</IsFed> 
<Name>acv4445-7</Name> 
</Animal> 

它應該出來裏ke:

<Animal type="hamster"> 
<IsFed>true</IsFed> 
<Name>Gerald</Name> 
</Animal> 
<Animal type="cow"> 
<IsFed>false</IsFed> 
<Name>acv4445-7</Name> 
</Animal> 
+0

看起來像一個微不足道的XSL轉換,因爲你說你已經在做一個轉換。什麼是問題? –

+0

@JimGarrison我在問怎麼做轉換,因爲我從來沒有使用過XLST。我問兩種方法來嘗試學習更多關於XSLT的知識。 –

回答

1
  1. 明確列出每個字段進行改造

XSLT 1.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:strip-space elements="*"/> 

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

<!-- list every boolean element here --> 
<xsl:template match="IsFed | HasShelter | etc. "> 
    <xsl:copy> 
     <xsl:value-of select=".='Y'"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

  • 自動檢測每一個這樣的有/無字段(I意識到這可能有錯誤)
  • XSLT 1.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:strip-space elements="*"/> 
    
    <!-- identity transform --> 
    <xsl:template match="@*|node()"> 
        <xsl:copy> 
         <xsl:apply-templates select="@*|node()"/> 
        </xsl:copy> 
    </xsl:template> 
    
    <xsl:template match="text()[.='Y']">true</xsl:template> 
    <xsl:template match="text()[.='N']">false</xsl:template> 
    
    </xsl:stylesheet> 
    
    1

    有趣的小問題。下面是XSLT 2(XSLT 1溶液下面向下)

    <?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        version="2.0"> 
        <xsl:template match="@*|node()"> 
         <xsl:copy> 
          <xsl:apply-templates select="@*|node()"/> 
         </xsl:copy> 
        </xsl:template> 
        <xsl:template match="*[starts-with(name(),'Is') and matches(text(),'[YN]')]/text()"> 
         <xsl:value-of select="if (.='Y') then 'true' else 'false'"></xsl:value-of> 
        </xsl:template> 
    </xsl:stylesheet> 
    

    這是恆等變換加上一個可能的解決方案的模板匹配的任何元素的名稱以Is開始並且其值是YN的文本,它取代truefalse。它不會影響名稱以Is的值開頭的元素,其值不是YN

    下面是XSLT同樣的事情1.0

    <?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        version="1.0"> 
        <xsl:template match="@*|node()"> 
         <xsl:copy> 
          <xsl:apply-templates select="@*|node()"/> 
         </xsl:copy> 
        </xsl:template> 
        <xsl:template match="*[substring(name(),1,2) = 'Is' and (text() = 'Y' or text() = 'N')]/text()"> 
         <xsl:choose> 
          <xsl:when test=". = 'Y'">true</xsl:when> 
          <xsl:otherwise>false</xsl:otherwise> 
         </xsl:choose> 
        </xsl:template> 
    </xsl:stylesheet>