這將是我第一次嘗試編寫一個實際上確實有重要意義的XSLT,這對我的工作有所幫助。我之前寫過xpath xpression,並且與他們相處得很好,只需要用XSLT弄溼我的腳,真正開始做飯。 Anywho,編寫一個XSLT以從XML文件中提取某些屬性
我有一個XML文件,其中有一些與value
屬性的節點。我想所有的節點與name
屬性在一個文本文件打印出自己的value
屬性...
這裏是我迄今爲止...示例XML
<?xml version="1.0"?>
<dataTemplateSpecification>
<templates>
<template>
<elements>
<element id="element0" name="PatientId" display="Patient ID" dataType="String" visable="true" readOnly="false" value="207">
<mapping path="//Template/TemplateData/ACOData/PATIENT_ID" />
<validation>
<rules>
<rule id="r0" test="#element0.value == ''">
<fail>
<html>
<b>Patient ID is null, value must be present</b>
</html>
</fail>
</rule>
</rules>
</validation>
</element>
<element id="element1" name="EncounterId" display="Encounter ID" dataType="String" visable="true" readOnly="false" value="144">
<mapping path="//Template/TemplateData/ACOData/FOCUSED_READMISSIONS_ID" />
<validation>
<rules>
<rule id="r0" test="#element0.value == ''">
<fail>
<html>
<b>Patient ID is null, value must be present</b>
</html>
</fail>
</rule>
</rules>
</validation>
</element>
</template></template></dataTemplateSpecification>
這是非常基本的我寫了XSLT ...
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="text" indent="yes"/>
<xsl:template match="//dataTemplateSpecification/templates/template/elements/element[@name=*]">
<xsl:copy>
<xsl:apply-templates select="@value"/>
</xsl:copy>
</xsl:template>
我會繼續在這個開裂,如果你們能在所有幫助我會永遠gratefu湖我當然會爲任何閱讀此內容的人發佈解決方案,如果我自己發現解決方案,也會對此問題產生興趣。謝謝。
DMainEvent:不是太糟糕的開始。以下是一些觀察結果:1.始終使用''的簡寫形式。寫:' '和* not *' xsl:value-of>'。2:模板匹配模式指定*相對* XPath表達式(很少包含多個位置步驟)。在你的情況下,它可能只是:'3.你可以使用一個XPath表達式(因此一個單一的')來指定多個比一個字符串:只需使用'concat()'函數。 –
2011-12-21 20:49:40