2012-09-12 1 views
2

XML 파일에는 레코드가 두 개 이상이고 값이 서로 다릅니다 (예 : 이름, 숫자, 가중치 등의 값이있는 항목 목록). XSLT를 사용하여 웹 페이지의 표 형태로 표시합니다. 모든 단일 페이지에는 다른 레코드가 표시됩니다. xslt는 웹 페이지의 매개 변수 값을 가져 와서 적절한 정보 만 표시합니다. 예를 들어 XYZ 항목에 관한 웹 페이지가 있습니다.이 특정 항목의 이름, 번호 및 무게 만 표시됩니다.XML 및 XSLT - XML에없는 매개 변수 값

제 질문은 XML에 그러한 항목이 없을 때 어떤 종류의 메시지 ("이 항목에는 데이터가 없습니다"와 같은)를 표시 할 수있는 방법이 있는지 의문입니다. 매개 변수가 비어 있거나 null 인 것과 달리 웹 페이지에서 가져옵니다. XML 파일에 그러한 레코드가 없다는 것입니다.

도움이 필요하십니까?

아래 XML 및 XLS 파일의 코드를 찾을 수 있습니다. 매개 변수/변수 이름과 값을 변경했지만 다른 모든 것은 원래 파일과 동일하게 유지됩니다.

XML :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<ItemsTable> 
<ItemRow> 
    <item>001</item> 
    <name>aaaa</name> 
    <price>2402</price> 
    <price2>2200</price2> 
</ItemRow> 
<ItemRow> 
    <item>002</item> 
    <name>bbbb</name> 
    <price>2402</price> 
    <price2>2700</price2> 
</ItemRow> 
<ItemRow> 
    <item>003</item> 
    <name>cccc</name> 
    <price>2402</price> 
    <price2>2003</price2> 
</ItemRow> 
<ItemRow> 
    <item>004</item> 
    <name>dddd</name> 
    <price>2402</price> 
    <price2>2024</price2> 
</ItemRow> 

XSL :

<xsl:template match="/ItemsTable/ItemRow [ item = $thisitem ]"> 

는 XSLT 1.0에서 불법이라는 사실을 제외하고

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:param name="thisitem">XXXX</xsl:param> 
<xsl:template match="/"> 
<xsl:apply-templates /> 
</xsl:template> 
<xsl:decimal-format name="european" decimal-separator="," grouping-separator="." /> 
<xsl:template match="/ItemsTable/ItemRow" /> 
<xsl:template match="/ItemsTable/ItemRow [ item = $thisitem ]"> 
<style>table.YYY { border-collapse: collapse; } table.YYY td, table.YYY th { border: 1px solid black; padding: 1em; vertical-align: middle; text-align: center; } table.YYY th { background-color: #eee; } table.YYY .header { font-size: 2em; font-weight: bold; padding-bottom: 1em; padding-top: 1em; } table.YYY .itemname { color: red; font-weight: bold; white-space: nowrap; } table.YYY .yellow { background-color: yellow; } table.YYY .red { background-color: red; } table.YYY .green { background-color: #40FF00; }</style> 
<html> 
<body> 
<table class="YYY"> 
<xsl:if test="string-length(name) > 0 and string-length(price) > 0 and string-length(price2) > 0" /> 
<xsl:choose> 
<xsl:when test="string-length(name) > 0 and string-length(price) > 0 and string-length(price2) > 0"> 
<tr> 
<th>Name</th> 
<th>Price 1</th> 
<th>Price 2</th> 
</tr> 
<tr> 
<xsl:choose> 
<xsl:when test="price > price2"> 
<td class="red"> 
<xsl:value-of select="name" /> 
</td> 
<td class="red"> 
<xsl:value-of select="format-number(price, '###.###.###', 'european')" /> 
</td> 
<td class="red"> 
<xsl:value-of select="format-number(price2, '###.###.###', 'european')" /> 
</td> 
</xsl:when> 
<xsl:otherwise> 
<td class="green"> 
    <xsl:value-of select="name" /> 
</td> 
<td class="green"> 
    <xsl:value-of select="format-number(price, '###.###.###', 'european')" /> 
</td> 
<td class="green"> 
    <xsl:value-of select="format-number(price2, '###.###.###', 'european')" /> 
</td> 
</xsl:otherwise> 
</xsl:choose> 
</tr> 
</xsl:when> 
<xsl:otherwise> 
<div> 
    <p>No data for this item</p> 
</div> 
</xsl:otherwise> 
</xsl:choose> 
</table> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 
+2

당신이 지금까지 가지고있는 XSLT 및 XML의 종류의 예를 보여줄 수 부모에 대한 템플릿에 최대 논리 "이 항목에 대한 데이터"를 이동하지합니다 당신은 입력으로 있나요? –

+0

물론입니다. 매개 변수/변수 이름과 값을 변경했지만 XML과 XSL은 아래의 것과 똑같이 작성됩니다. – Marta

+0

"아래"에 아무것도 없습니다. –

답변

0

(당신은 사용할 수 없습니다 스펙 당 일치 표현식의 변수, 그러나 몇몇 가공업자는 어쨌든 그것을 받아 들인다), 당신의 문제는 그 (것)들이 일치하는 성분이 존재할 경우에만 템플렛이 점화된다이다. item 자식이 요청하는 자식이없는 ItemRow이 없으면 템플릿이 전혀 실행되지 않습니다.

는 대신 ItemsTable

<xsl:template match="/ItemsTable"> 
    <xsl:variable name="matchingRows" select="ItemRow[item = $thisitem][string-length(name)][string-length(price)][string-length(price2)]"/> 
    <xsl:choose> 
    <xsl:when test="$matchingRows"> 
     <xsl:apply-templates select="$matchingRows"/> 
    </xsl:when> 
    <xsl:otherwise> 
     <div> 
     <p>No data for this item</p> 
     </div> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<xsl:template match="ItemRow"> 
    <!-- logic for each matching row here, don't need to check the 
     preconditions as the template is only called for rows that match --> 
</xsl:template> 
+0

감사합니다! 매력처럼 작동 :) – Marta