2_xml_structuring_best_practices.xml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <xml_structuring_best_practices>
  2. <overview>
  3. XML tags help Claude parse prompts more accurately, leading to higher-quality outputs.
  4. This guide covers best practices for structuring mode instructions using XML.
  5. </overview>
  6. <why_use_xml_tags>
  7. <benefit type="clarity">
  8. Clearly separate different parts of your instructions and ensure well-structured content
  9. </benefit>
  10. <benefit type="accuracy">
  11. Reduce errors caused by Claude misinterpreting parts of your instructions
  12. </benefit>
  13. <benefit type="flexibility">
  14. Easily find, add, remove, or modify parts of instructions without rewriting everything
  15. </benefit>
  16. <benefit type="parseability">
  17. Having Claude use XML tags in its output makes it easier to extract specific parts of responses
  18. </benefit>
  19. </why_use_xml_tags>
  20. <core_principles>
  21. <principle name="consistency">
  22. <description>Use the same tag names throughout your instructions</description>
  23. <example>
  24. Always use <step> for workflow steps, not sometimes <action> or <task>
  25. </example>
  26. </principle>
  27. <principle name="semantic_naming">
  28. <description>Tag names should clearly describe their content</description>
  29. <good_examples>
  30. <tag>detailed_steps</tag>
  31. <tag>error_handling</tag>
  32. <tag>validation_rules</tag>
  33. </good_examples>
  34. <bad_examples>
  35. <tag>stuff</tag>
  36. <tag>misc</tag>
  37. <tag>data1</tag>
  38. </bad_examples>
  39. </principle>
  40. <principle name="hierarchical_nesting">
  41. <description>Nest tags to show relationships and structure</description>
  42. <example>
  43. <workflow>
  44. <phase name="preparation">
  45. <step>Gather requirements</step>
  46. <step>Validate inputs</step>
  47. </phase>
  48. <phase name="execution">
  49. <step>Process data</step>
  50. <step>Generate output</step>
  51. </phase>
  52. </workflow>
  53. </example>
  54. </principle>
  55. </core_principles>
  56. <common_tag_patterns>
  57. <pattern name="workflow_structure">
  58. <usage>For step-by-step processes</usage>
  59. <template><![CDATA[
  60. <workflow>
  61. <overview>High-level description</overview>
  62. <prerequisites>
  63. <prerequisite>Required condition 1</prerequisite>
  64. <prerequisite>Required condition 2</prerequisite>
  65. </prerequisites>
  66. <steps>
  67. <step number="1">
  68. <title>Step Title</title>
  69. <description>What this step accomplishes</description>
  70. <actions>
  71. <action>Specific action to take</action>
  72. </actions>
  73. <validation>How to verify success</validation>
  74. </step>
  75. </steps>
  76. </workflow>
  77. ]]></template>
  78. </pattern>
  79. <pattern name="examples_structure">
  80. <usage>For providing code examples and demonstrations</usage>
  81. <template><![CDATA[
  82. <examples>
  83. <example name="descriptive_name">
  84. <description>What this example demonstrates</description>
  85. <context>When to use this approach</context>
  86. <code language="typescript">
  87. // Your code example here
  88. </code>
  89. <explanation>
  90. Key points about the implementation
  91. </explanation>
  92. </example>
  93. </examples>
  94. ]]></template>
  95. </pattern>
  96. <pattern name="guidelines_structure">
  97. <usage>For rules and best practices</usage>
  98. <template><![CDATA[
  99. <guidelines category="category_name">
  100. <guideline priority="high">
  101. <rule>The specific rule or guideline</rule>
  102. <rationale>Why this is important</rationale>
  103. <exceptions>When this doesn't apply</exceptions>
  104. </guideline>
  105. </guidelines>
  106. ]]></template>
  107. </pattern>
  108. <pattern name="tool_usage_structure">
  109. <usage>For documenting how to use specific tools</usage>
  110. <template><![CDATA[
  111. <tool_usage tool="tool_name">
  112. <purpose>What this tool accomplishes</purpose>
  113. <when_to_use>Specific scenarios for this tool</when_to_use>
  114. <syntax>
  115. <command>The exact command format</command>
  116. <parameters>
  117. <parameter name="param1" required="true">
  118. <description>What this parameter does</description>
  119. <type>string|number|boolean</type>
  120. <example>example_value</example>
  121. </parameter>
  122. </parameters>
  123. </syntax>
  124. <examples>
  125. <example scenario="common_use_case">
  126. <code>Actual usage example</code>
  127. <output>Expected output</output>
  128. </example>
  129. </examples>
  130. </tool_usage>
  131. ]]></template>
  132. </pattern>
  133. </common_tag_patterns>
  134. <formatting_guidelines>
  135. <guideline name="indentation">
  136. Use consistent indentation (2 or 4 spaces) for nested elements
  137. </guideline>
  138. <guideline name="line_breaks">
  139. Add line breaks between major sections for readability
  140. </guideline>
  141. <guideline name="comments">
  142. Use XML comments <!-- like this --> to explain complex sections
  143. </guideline>
  144. <guideline name="cdata_sections">
  145. Use CDATA for code blocks or content with special characters:
  146. <![CDATA[<code><![CDATA[your code here]]></code>]]>
  147. </guideline>
  148. <guideline name="attributes_vs_elements">
  149. Use attributes for metadata, elements for content:
  150. <example type="good">
  151. <step number="1" priority="high">
  152. <description>The actual step content</description>
  153. </step>
  154. </example>
  155. </guideline>
  156. </formatting_guidelines>
  157. <anti_patterns>
  158. <anti_pattern name="flat_structure">
  159. <description>Avoid completely flat structures without hierarchy</description>
  160. <bad><![CDATA[
  161. <instructions>
  162. <item1>Do this</item1>
  163. <item2>Then this</item2>
  164. <item3>Finally this</item3>
  165. </instructions>
  166. ]]></bad>
  167. <good><![CDATA[
  168. <instructions>
  169. <steps>
  170. <step order="1">Do this</step>
  171. <step order="2">Then this</step>
  172. <step order="3">Finally this</step>
  173. </steps>
  174. </instructions>
  175. ]]></good>
  176. </anti_pattern>
  177. <anti_pattern name="inconsistent_naming">
  178. <description>Don't mix naming conventions</description>
  179. <bad>
  180. Mixing camelCase, snake_case, and kebab-case in tag names
  181. </bad>
  182. <good>
  183. Pick one convention (preferably snake_case for XML) and stick to it
  184. </good>
  185. </anti_pattern>
  186. <anti_pattern name="overly_generic_tags">
  187. <description>Avoid tags that don't convey meaning</description>
  188. <bad>data, info, stuff, thing, item</bad>
  189. <good>user_input, validation_result, error_message, configuration</good>
  190. </anti_pattern>
  191. </anti_patterns>
  192. <integration_tips>
  193. <tip>
  194. Reference XML content in instructions:
  195. "Using the workflow defined in &lt;workflow&gt; tags..."
  196. </tip>
  197. <tip>
  198. Combine XML structure with other techniques like multishot prompting
  199. </tip>
  200. <tip>
  201. Use XML tags in expected outputs to make parsing easier
  202. </tip>
  203. <tip>
  204. Create reusable XML templates for common patterns
  205. </tip>
  206. </integration_tips>
  207. </xml_structuring_best_practices>