我在專案中作業,在那里我獲得了允許的字串列,并需要洗掉不需要的字符。我已經完成了以下操作,但我覺得它很麻煩,而且應該是
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="follow">0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ?abcdefghijklmnopqrstuvwxyz-'.,/@&()! </xsl:variable>
<xsl:variable name="start">0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ?abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:template match="/">
<html>
<body>
<xsl:choose>
<xsl:when test="contains($start, substring(normalize-space(/Author/Name/FirstName),1,1)) and
string-length(substring(normalize-space(/Author/Name/FirstName),1,1)) > 0 and
string-length(translate(substring(normalize-space(/Author/Name/FirstName),2),translate(substring(normalize-space(/Author/Name/FirstName),2),$follow,''),'')) > 0">
<div>
<xsl:value-of select="translate(substring(normalize-space(/Author/Name/FirstName),1),
translate(substring(normalize-space(/Author/Name/FirstName),1),$follow,''),'')" />
</div>
</xsl:when>
<xsl:otherwise>NULL</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
測驗開始條件我添加了三個檢查。對于空字串大小寫,包含檢查回傳 true,因此我添加了字串長度條件,以便為空字串大小寫回傳 NULL。
FirstName>? #</FirstName>//NULL
<FirstName></FirstName>//NULL
<FirstName> ??</FirstName>//??
<LastName>?t*#</LastName>//?t
我用于測驗的 XML 如下
<?xml version="1.0" encoding="UTF-8"?>
<Author>
<Name>
<FirstName>xxx</FirstName>
</Name>
</Author>
我可能錯過了任何邊緣情況,我的問題是有沒有更好的方法來解決這個 XSLT 過濾任務,其中開始和連續字符是有條件的?
編輯 閱讀 michael.hor257k 評論讓我質疑我的方法并更了解我的要求。有Cyber??source頁面,它在向其 api 發出請求時指定允許的字符。我的目標是清除不需要的字符,并確保該欄位的開頭和后面的字符符合網站上給出的規范。以收貨方公司名稱為例。我將 XSLT 1.0 與 java Transformer 類一起使用
uj5u.com熱心網友回復:
考慮以下簡化示例:
XML
<input>
<item>alpha</item>
<item>-alpha</item>
<item>alp§ha</item>
<item>---al§pha§</item>
<item>§al-pha</item>
</input>
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:variable name="allowed-start-chars">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="allowed-follow-chars">abcdefghijklmnopqrstuvwxyz-</xsl:variable>
<xsl:template match="/input">
<output>
<xsl:apply-templates/>
</output>
</xsl:template>
<xsl:template match="item">
<!-- find the first character eligible to be starting character -->
<xsl:variable name="start-chars" select="translate(., translate(., $allowed-start-chars, ''), '')"/>
<xsl:variable name="start-char" select="substring($start-chars, 1, 1)"/>
<!-- get text after the chosen starting character -->
<xsl:variable name="tail" select="substring-after(., $start-char)"/>
<result original="{.}">
<xsl:value-of select="$start-char"/>
<!-- remove unwanted characters from tail -->
<xsl:value-of select="translate($tail, translate($tail, $allowed-follow-chars, ''), '')"/>
</result>
</xsl:template>
</xsl:stylesheet>
結果
<?xml version="1.0" encoding="UTF-8"?>
<output>
<result original="alpha">alpha</result>
<result original="-alpha">alpha</result>
<result original="alp§ha">alpha</result>
<result original="---al§pha§">alpha</result>
<result original="§al-pha">al-pha</result>
</output>
您可能想要為所有字符都被證明是非法的情況添加一個測驗 - 盡管這似乎不太可能。
添加:
如果您只想測驗輸入是否有效,那么您可以執行以下操作:
<xsl:template match="item">
<!-- test the first character -->
<xsl:variable name="valid-start-char" select="contains($allowed-start-chars, substring(., 1, 1))"/>
<!-- test following characters -->
<xsl:variable name="invalid-follow-chars" select="translate(substring(., 2), $allowed-follow-chars, '')"/>
<result original="{.}">
<xsl:choose>
<xsl:when test="$valid-start-char and not($invalid-follow-chars)">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>NULL</xsl:otherwise>
</xsl:choose>
</result>
</xsl:template>
要得到:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<result original="alpha">alpha</result>
<result original="-alpha">NULL</result>
<result original="alp§ha">NULL</result>
<result original="---al§pha§">NULL</result>
<result original="§al-pha">NULL</result>
</output>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/331661.html
