2016-03-30 5 views
0

私は非常に奇妙なXMLファイルを扱っています。これはライブラリで最も人気のある書籍のリストです。計算された比率のXMLフィルタリスト

タイトル2つのアイテムに4つのホールドがあり、2つのホールド/アイテムの割合が与えられます。 タイトル2には、1つのアイテムに5つのホールドがあり、4つのホールド/アイテムの割合が与えられ、タイトル3には5つのホールド/アイテムの割合があります。

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="holdratio3.xsl"?> 
<report> 
<title>Individual Item Display</title> 
<dateCreated>2016-03-29T16:58:57</dateCreated> 
<dateFormat>yyyy/mm/dd</dateFormat> 
<catalog> 
    <marc> 
     <marcEntry tag="245" label="Title" ind="10">Title one</marcEntry> 
    </marc> 
    <call> 
     <item> 
      <numberOfHolds>4</numberOfHolds> 
      <type>LOAN</type> 
     </item> 
     <item> 
      <numberOfHolds>4</numberOfHolds> 
      <type>LOAN</type> 
     </item> 
    </call> 
</catalog> 
<catalog> 
    <marc> 
     <marcEntry tag="245" label="Title" ind="10">Title two</marcEntry> 
    </marc> 
    <call> 
     <item> 
      <numberOfHolds>5</numberOfHolds> 
      <type>LOAN</type> 
     </item> 
    </call> 
</catalog> 
<catalog> 
    <marc> 
     <marcEntry tag="245" label="Title" ind="10">Title three</marcEntry> 
    </marc> 
    <call> 
     <item> 
      <numberOfHolds>4</numberOfHolds> 
      <type>LOAN</type> 
     </item> 
    </call> 
</catalog> 
</report> 

私はこのようになりますスタイルシートを持っている - そしてそれは意図した結果 Hold ratio outputを与えます。 これをもう少し洗練させることができないのではないかと思います。 比率を3以上にすると結果がフィルタリングされます。 私はグーグルではありましたが、解決策を見つけることができませんでしたが、それはfor-eachまたはapply-templateのどちらかと思われます。

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/"> 
    <catalog> 
    <xsl:for-each select="report/catalog"> 
    <xsl:sort select="call/item/numberOfHolds div count(call/item [type='LOAN'])" order="descending" /> 
    <ratioline> 
     <Title><xsl:value-of select="substring(marc/marcEntry[@tag='245'],1,30)"/></Title> 
     <numberOfHolds><xsl:value-of select="call/item/numberOfHolds"/></numberOfHolds> 
     <numberOfItemsSelectedType><xsl:value-of select="count(call/item[type='LOAN'])"/></numberOfItemsSelectedType> 
      <ratio><xsl:value-of select="format-number(call/item/numberOfHolds div count(call/item[type='FLYTANDE' or type='HEMLAN']), '00.##')" /></ratio> 
     </ratioline> 
    </xsl:for-each> 
    </catalog> 
</xsl:template> 
</xsl:stylesheet> 
+0

このXSLTをお試しください2つのアイテムがあり、それぞれに独自の保留価値があります。どちらを選択するのですか?なぜですか? –

+0

はい、私は知っています - それは私たちの図書館システムから来る奇妙な価値です。タイトルには4つのホールドがありますが、値はアイテムごとに繰り返されます。不正なフォーマットの良い例:-( –

答えて

0

あなたはそれをフィルタリングするには3

<xsl:param name="min" select="3" /> 

のあなたの価値を保持するためのパラメータを定義することで始めることができ、あなたは最初の比率を保持するためにxsl:for-each内の変数を定義することができます

<xsl:variable name="ratio" select="call/item/numberOfHolds div count(call/item [type='LOAN'])" /> 

xsl:ifを使用して、比率がパラメータと等しいか大きいかをテストできます。

​​

なぜ4つの "タイトル1は、4 * 2つの項目に保持しています*"?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 
<xsl:param name="min" select="3" /> 
<xsl:template match="/"> 
    <catalog> 
    <xsl:for-each select="report/catalog"> 
    <xsl:sort select="call/item/numberOfHolds div count(call/item [type='LOAN'])" order="descending" /> 
     <xsl:variable name="ratio" select="call/item/numberOfHolds div count(call/item [type='LOAN'])" /> 
     <xsl:if test="$ratio >= $min"> 
     <ratioline> 
      <Title><xsl:value-of select="substring(marc/marcEntry[@tag='245'],1,30)"/></Title> 
      <numberOfHolds> 
       <xsl:value-of select="call/item/numberOfHolds"/> 
      </numberOfHolds> 
      <numberOfItemsSelectedType> 
       <xsl:value-of select="count(call/item[type='LOAN'])"/> 
      </numberOfItemsSelectedType> 
      <ratio> 
       <xsl:value-of select="format-number($ratio, '00.##')" /> 
      </ratio> 
     </ratioline> 
     </xsl:if> 
    </xsl:for-each> 
    </catalog> 
</xsl:template> 
</xsl:stylesheet> 
関連する問題